简体   繁体   English

如何在Python中跳过Excel工作表的前几行

[英]How to skip first several lines of Excel sheet in Python

I am able to successfully un-merge all cells in Excel sheet using openpyxl ; 我可以使用openpyxl成功取消合并Excel工作表中的所有单元格; however, I would like to keep the first 7 lines of the sheet intact. 但是,我想保持工作表的前7行不变。 As shown below, the first 7 lines contain merged cells. 如下所示,前7行包含合并的单元格。

上excel表格

After I run the following code (which finds merged cells and splits them): 在运行以下代码(查找合并的单元格并将其拆分)之后:

def fill_in(rows,first_cell,last_cell):
    #Take first cell's value
    first_value = first_cell.value
    #Copy and fill/assign this value into each cell of the range
    for tmp in rows:  
        cell = tmp[0]
        print(cell) ##E.g. (<Cell 'Sheet1'.A1>,)  
        print(cell.value) ##E.g. Order Records
        cell.value = first_value 

wb2 = load_workbook('Example.xlsx')
sheets = wb2.sheetnames ##list of sheetnames
for i,sheet in enumerate(sheets): ##for each sheet
    ws = wb2[sheets[i]]
    range_list = ws.merged_cell_ranges
    for _range in range_list:
        first_cell = ws[_range.split(':')[0]] #first cell of each range
        last_cell = ws[_range.split(':')[1]]
        rows = ws[_range] #big set of sets; each cell within each range
        fill_in(list(rows),first_cell,last_cell)   

For reference, rows looks like this: 供参考, rows如下所示:
((<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.B1>, <Cell 'Sheet1'.C1>, <Cell 'Sheet1'.D1>, <Cell 'Sheet1'.E1>),)

This is what the new Excel sheet looks like: the top 7 lines get disordered and messy. 这就是新的Excel工作表的样子: 前7行混乱无序。

上层Excel工作表的结果

Considering my code above, what can I include/do to skip the first 7 lines of Excel sheet or exclude those lines from being un-merged? 考虑到上面的代码,我可以包括/执行什么操作以跳过Excel工作表的前7行,或者将这些行排除在未合并之外?

merged_cell_ranges是一个列表,因此您只需要在索引30(6行* 5列)之后开始迭代即可

for _range in range_list[30:]:

This worked for me. 这对我有用。

import pandas as pd
xls = pd.ExcelFile('C:/Users/rschuell/Desktop/Data.xlsx')
df = xls.parse('Data', skiprows=4, index_col=None, na_values=['NA'])
print(df)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM