首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在合并的单元格中获得值?

如何在合并的单元格中获得值?
EN

Stack Overflow用户
提问于 2014-05-09 18:30:38
回答 7查看 14.1K关注 0票数 7

我想使用openpyxl库获取一个合并后的单元格的值,它的范围从D3到H3。据我所知,大多数库从第一个单元格本身读取数据。因此,合并后的内容存在于其中,但当我读取它时,我会得到一个none值。

以下是我的代码:

代码语言:javascript
复制
wb = load_workbook(work_directory_path+'/outputs/report_vap.xlsx')
ws = wb.get_sheet_by_name("Summary")
suite_path = ws.cell('D3').value
if not isinstance(suite_path, unicode):
    value=unicode(suite_path)
value=value.encode('utf8')
print "Suite Location is "+value;

输出为:

代码语言:javascript
复制
Suite Location is None

D3 to H3的单元格中的值为:

代码语言:javascript
复制
c:\users\xyz\desktop\abc\c++\events\comevents

我甚至尝试打印工作表中的所有值,但除了整数值之外,所有值都返回None。

以下是更改后的代码:

代码语言:javascript
复制
wb = load_workbook(work_directory_path+'/outputs/report_vap.xlsx')
ws = wb.get_sheet_by_name("Summary")
for row_index in range (ws.get_highest_row()):
    for col_index in range (ws.get_highest_column()):
        print ws.cell(row=row_index, column=col_index).value
suite_path = ws.cell('A11').value
print suite_path
if not isinstance(suite_path, unicode):
   value=unicode(suite_path)
value=value.encode('utf8')
print "Suite Location is "+value;

输出为:

代码语言:javascript
复制
None


None
None
None
None
None
None
None
None
None
None
None
None
None
None


None




None


None




None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None


None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
1
1
None
None
None
None
None
None
9
1106
None
None
None
None
None
None
10
1107
None
None
None
None
None
None
None
None
None
None
Suite Location is None
Suite Location is None
12

Excel文件包含以下内容:

代码语言:javascript
复制
Project/module ID   Project/module  Build Analysis  Language    Compiler    Source File Source File

1_1 HTMLEdit.vcxproj    Success C++ Microsoft Visual Studio 2010 ( version 10.0 )   1   1

1_2 HTMLEdit.vcxproj    Success C++ Microsoft Visual Studio 2010 ( version 10.0 )   9   1106
Total                   10  1107
EN

回答 7

Stack Overflow用户

发布于 2015-12-22 22:39:41

一旦唯一的答案是不正确的(在openpyxl中没有更多的cells_from_range函数),我建议使用另一种方法。我试过了,这对我的情况很有效:

输入为工作表和单元格。但是如果你需要的话,你可以很容易的修改它来接受像'A3‘这样的字符串单元表示。

代码语言:javascript
复制
import openpyxl


def getValueWithMergeLookup(sheet, cell):
    idx = cell.coordinate
    for range_ in sheet.merged_cell_ranges:
        merged_cells = list(openpyxl.utils.rows_from_range(range_))
        for row in merged_cells:
            if idx in row:
                # If this is a merged cell,
                # return  the first cell of the merge range
                return sheet.cell(merged_cells[0][0]).value

    return sheet.cell(idx).value
票数 12
EN

Stack Overflow用户

发布于 2020-12-26 17:10:03

我是基于Openpyxl的最新源代码编写的:

代码语言:javascript
复制
def getMergedCellVal(sheet, cell):
    rng = [s for s in sheet.merged_cells.ranges if cell.coordinate in s]
    return sheet.cell(rng[0].min_row, rng[0].min_col).value if len(rng)!=0 else cell.value
票数 7
EN

Stack Overflow用户

发布于 2014-11-25 06:53:14

下面是我使用的函数的近似值:

代码语言:javascript
复制
from openpyxl.cell import get_column_letter
from openpyxl.worksheet import cells_from_range

def getValueWithMergeLookup(sheet, col, row):
    idx = '{0}{1}'.format(get_column_letter(col), row)
    for range_ in sheet.merged_cell_ranges:
        cells = list(cells_from_range(range_))[0]
        if idx in cells:
            # If this is a merged cell, you can look up the value 
            # in the first cell of the merge range
            return sheet.cell(cells[0]).value

    return sheet.cell(row=row, column=col).value

唯一真正危险的地方是在哪里提取要搜索的范围内的单元格列表。这将返回一个生成器,所以我将其转换为一个列表(因为in显然不适用于生成器),它产生一个包含单个列表元素的元组,我使用0索引提取该元素。

就我的目的而言,这已经足够快了--我通过迭代要测试的单元来使用它。如果您希望提高性能,可能值得反转循环,将合并范围作为外部循环进行迭代,这样您只需执行一次转换。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23562366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档