【发布时间】:2019-07-24 16:28:45
【问题描述】:
我正在尝试从具有 merge_cells_range 的 excel 文件中读取数据...但输出不是我的目标。请帮帮我
import openpyxl
wb = openpyxl.load_workbook('book1.xlsx')
sheet = wb.get_sheet_by_name('info')
all_data=[]
print(sheet.merged_cells.ranges)
for row_index in range(1,sheet.max_row+1):
row=[]
for col_index in range(1,sheet.max_column+1):
vals = sheet.cell(row_index,col_index).value
if vals =='':
for crange in sheet.merged_cells.ranges:
rlo,rhi,clo,chi = crange
if rlo<=row_index and row_index<rhi and clo<=col_index and col_index<chi:
vals = sheet.cell(rlo,clo).value
print(vals)
break
row.append(vals)
all_data.append(row)
print(all_data)
for row in all_data:
sheet.append(row)
wb.save('bbbb.xlsx')
我希望得到输出: [['06B', '大宇 BC 212', 80, 1373], ['06C', '大宇 BC 212', 80, 1020], ['06D', 'Transinco B60KL', 60, 1061], [' 06D', 'Transinco B60KL', 60, 19], ['06E', '大宇 BC 212', 80, 1020], ['06E', '大宇 BC 212', 60, 1061], ['06E', 'Daewoo BC 212', 60, 19]] 但结果是:
[['06B', '大宇 BC 212', 80, 1373], ['06C', '大宇 BC 212', 80, 1020], ['06D', 'Transinco B60KL', 60, 1061] , [无, 无, 60, 19], ['06E', '大宇 BC 212', 80, 1020], [无, 无, 60, 1061], [无, 无, 60, 19]]
【问题讨论】:
-
您可能会发现取消合并相关范围并相应地复制列更容易。