[英]openpyxl is exporting excel file with error
I have an excel file where the first four rows contain some header text and the actual dataset starts from row 4. I am trying to build a simple function that reads the excel file and outputs the same excel file after deleting the first 4 rows.
这是我的代码在放入 function 之前的样子。
import pandas as pd
from openpyxl import load_workbook, Workbook
wb = load_workbook('FILEPATH/excel.xlsx')
ws = wb['Sheet1']
ws = ws.delete_rows(0,4)
wb.save(r"FILEPATH/deleted_row.xlsx")
当我运行代码时,它会正确执行文件,但是当我尝试打开 excel 文件时,它会给我错误并说文件已损坏。 需要注意的一点是 excel 文件在 rop 行上有一些格式。 这是造成一些问题的原因吗?
任何帮助表示赞赏。
在 openpyxl 中,第一行应该是 1,而不是 0。因此,如果您要删除前 4 行,您应该将delete_row()
从
ws = ws.delete_rows(0,4)
至
ws = ws.delete_rows(1,4)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.