我必须处理从外部来源收到的一些xlsx
。有没有更直接的方法可以在xlsx
中加载pandas
的同时跳过带有删除线的行?
目前,我必须做这样的事情:
import pandas as pd, openpyxl
working_file = r"something.xlsx"
working_wb = openpyxl.load_workbook(working_file, data_only=True)
working_sheet = working_wb.active
empty = []
for row in working_sheet.iter_rows("B", row_offset=3):
for cell in row:
if cell.font.strike is True:
p_id = working_sheet.cell(row=cell.row, column=37).value
empty.append(p_id)
df = pd.read_excel(working_file, skiprows=3)
df = df[~df["ID"].isin(empty)]
...
这是可行的,但只能通过每个Excel工作表两次。
在这种情况下,我不会使用熊猫。只需使用openpyxl,从工作表的end开始工作,然后相应地删除行。从工作表的末尾开始进行反向工作意味着删除行时不会出现副作用。