簡體   English   中英

如何使用openpyxl在python中讀取合並的單元格?

[英]How to read merged cells in python using openpyxl?

我正在嘗試從具有merged_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','Daewoo BC 212',80,1373],['06C','Daewoo BC 212',80,1020],['06D','Transinco B60KL',60 ,1061],['06D','Transinco B60KL',60,19],['06E','Daewoo BC 212',80,1020],['06E','Daewoo BC 212',60,1061] ,['06E','Daewoo BC 212',60,19]],但結果是:

[['06B','Daewoo BC 212',80,1373],['06C','Daewoo BC 212',80,1020],['06D','Transinco B60KL',60,1061],[無,無,60、19],['06E','大宇BC 212',80、1020],[無,無,60、1061],[無,無,60、19]]

我的輸入 輸出期望

你去了:

= ^ .. ^ =

import openpyxl
from openpyxl import Workbook

# load data
raw_data = openpyxl.load_workbook('data.xlsx')
select_sheet = raw_data['Sheet1']


# collect data from rows
valid_row = []
data = []
for row in select_sheet.iter_rows(max_row=select_sheet.max_row, max_col=select_sheet.max_column):
    # get cell values
    row_data = [cell.value for cell in row]

    # handle merged cells
    new_row_data = [0]*select_sheet.max_column
    if None in row_data:
        new_row_data[0] = valid_row[0]
        new_row_data[1] = valid_row[1]
        new_row_data[2] = row_data[2]
        new_row_data[3] = row_data[3]
        data.append(new_row_data)
    else:
        data.append(row_data)
    # storage valid row
    if None not in row_data:
        valid_row = row_data


# save data
book = Workbook()
new_sheet = book.active

for row in data:
    new_sheet.append(row)

book.save('new_data.xlsx')

輸入:

     0    1    2    3
0    B   212   80  1.2
1    C   212   80  1.3
2    D   B60   60  1.4
3  None  None  60  1.5
4    E   212   80  1.6
5  None  None  60  1.7
6  None  None  60  1.8

輸出:

   0    1   2    3
0  B  212  80  1.2
1  C  212  80  1.3
2  D  B60  60  1.4
3  D  B60  60  1.5
4  E  212  80  1.6
5  E  212  60  1.7
6  E  212  60  1.8

我修改了代碼,它可以正常工作。

import openpyxl
from openpyxl.utils import range_boundaries
wb = openpyxl.load_workbook('book1.xlsx')
sheet = wb.get_sheet_by_name('info')
all_data=[]
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 == None:
            for crange in sheet.merged_cells:
                clo,rlo,chi,rhi = crange.bounds
                top_value = sheet.cell(rlo,clo).value
                if rlo<=row_index and row_index<=rhi and clo<=col_index and col_index<=chi:
                vals = top_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')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM