import pandas as pdimport osimport refrom openpyxl import Workbook# 读取指定路径下的CSV文件folder_path = 自动验单数据自动验单发生率四月file_format = r自动验单发生率统计详情d8csvoutput_folder = F自动验单数据45-55验本数据output_file = Nonefor filen
The error "AttributeError: can't set attribute 'sheets'" is occurring because the writer object does not have an attribute named sheets.
To fix this issue, you can modify the code as follows:
if output_file is None:
output_file = os.path.join(output_folder, file_name)
data.to_excel(output_file, index=False)
else:
with pd.ExcelWriter(output_file, mode='a', engine='openpyxl') as writer:
writer.book = Workbook()
writer.sheets = {ws.title: ws for ws in writer.book.worksheets}
data.to_excel(writer, index=False, header=False,
startrow=writer.sheets["Sheet1"].max_row)
writer.save()
This code creates a new Workbook object and assigns it to the writer.book attribute. Then, the sheets attribute is set to a dictionary containing the existing worksheets in the workbook. This allows the code to append the data to the existing worksheets in the file.
Note: Make sure to import the Workbook class from openpyxl at the beginning of the code:
from openpyxl import Workbook
``
原文地址: https://www.cveoy.top/t/topic/ihx0 著作权归作者所有。请勿转载和采集!