The error "AttributeError: can't set attribute 'sheets'" is occurring because the writer object does not have an attribute named sheets. \n\nTo fix this issue, you can modify the code as follows:\n\npython\nif output_file is None:\n output_file = os.path.join(output_folder, file_name)\n data.to_excel(output_file, index=False)\nelse:\n with pd.ExcelWriter(output_file, mode='a', engine='openpyxl') as writer:\n writer.book = Workbook()\n writer.sheets = {ws.title: ws for ws in writer.book.worksheets}\n data.to_excel(writer, index=False, header=False,\n startrow=writer.sheets["Sheet1"].max_row)\n writer.save()\n\n\nThis 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.\n\nNote: Make sure to import the Workbook class from openpyxl at the beginning of the code:\n\npython\nfrom openpyxl import Workbook\n

解决

原文地址: https://www.cveoy.top/t/topic/pZML 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录