我想使用openpyxl库获取一个合并后的单元格的值,它的范围从D3到H3。据我所知,大多数库从第一个单元格本身读取数据。因此,合并后的内容存在于其中,但当我读取它时,我会得到一个none值。
以下是我的代码:
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;输出为:
Suite Location is NoneD3 to H3的单元格中的值为:
c:\users\xyz\desktop\abc\c++\events\comevents我甚至尝试打印工作表中的所有值,但除了整数值之外,所有值都返回None。
以下是更改后的代码:
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;输出为:
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
12Excel文件包含以下内容:
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发布于 2015-12-22 22:39:41
一旦唯一的答案是不正确的(在openpyxl中没有更多的cells_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发布于 2020-12-26 17:10:03
我是基于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发布于 2014-11-25 06:53:14
下面是我使用的函数的近似值:
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索引提取该元素。
就我的目的而言,这已经足够快了--我通过迭代要测试的单元来使用它。如果您希望提高性能,可能值得反转循环,将合并范围作为外部循环进行迭代,这样您只需执行一次转换。
https://stackoverflow.com/questions/23562366
复制相似问题