如何在合并的单元格中显示值

man*_*sha 6 excel python-2.7 openpyxl

我想使用openpyxl库获得从D3到H3的合并单元格的值.根据我的理解,大多数图书馆从第一个细胞本身读取数据.因此合并的内容存在于其中,但是当我阅读它时,我得到一个无值.

以下是我的代码:

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;
Run Code Online (Sandbox Code Playgroud)

输出为:套房位置为无

D3到H3的单元格中的值为:c:\ users\xyz\desktop\abc\c ++\events\comevents

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

以下是更改的代码:

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;
Run Code Online (Sandbox Code Playgroud)

输出是:

没有

当作没有没有没有没有没有没有没有没有没有没有没人

没有

没有

没有

当晚红潮无谁没有没有没有没有没有没有没有没有没有没人

宣宣者者和目图(连续作品1)1 0作品无无无无无9 916无性无无无无10 1011 2007无无无无套房位置(无)套房位置为无12

Excel文件包含以下内容

项目/模块ID项目/模块构建分析语言编译器源文件源文件

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

1_2 HTMLEdit.vcxproj Success C++ Microsoft Visual Studio 2010(版本10.0)9 1106总计10 1107

The*_*her 10

一旦唯一的答案是不正确的(openpyxl中没有更多的cell_from_range函数)我建议替代方法.我试过,它适用于我的情况:

输入是表格和单元格.但如果需要,可以很容易地修改它以接受像'A3'这样的字符串单元格表示.

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
Run Code Online (Sandbox Code Playgroud)


Sha*_*ady 6

我是根据 Openpyxl 的最新源代码编写的:

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
Run Code Online (Sandbox Code Playgroud)


Tim*_*ing 2

这是我为此使用的函数的近似值:

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
Run Code Online (Sandbox Code Playgroud)

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

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