打开输出文件时ExcelWriter对齐中心错误

ExcelWriter align center error when opening output file

此代码运行良好:

excel_path = os.path.join(path,'Report For Yr '+str(date.today().year)+' Mth ' +mth+'.xlsx')

writer = pd.ExcelWriter(excel_path, engine='xlsxwriter')

df2.to_excel(writer, sheet_name='report')

workbook  = writer.book

worksheet = writer.sheets['report']

format1 = workbook.add_format({'num_format': '#,###'})

format2 = workbook.add_format({'num_format': '0.00%'})

format3 = workbook.add_format({'num_format': '#,##0.00'})

worksheet.set_column('B:I', 10, format1)

worksheet.set_column('J:J', 10, format2)

worksheet.set_column('L:L', 10, format2)

worksheet.set_column('K:K', 10, format3)

writer.save() 

我正在尝试使某些列居中。代码运行没有任何错误但是打开excel文件时出现错误:

Excel 无法打开文件,因为文件格式或文件扩展名无效。验证文件没有损坏,文件扩展名是否与文件格式匹配。

我尝试使用工作代码并更改此代码:

format1 = workbook.add_format({'num_format': '#,###'},{'align': 'center'})

收到错误信息

我尝试使用工作代码并更改此代码:

worksheet.set_column('B:I', 10, format1,{'align': 'center'})

也收到错误信息

有人可以帮忙吗?

I tried with the working code and change this:

这不是正确的语法。你应该传递一个单一的值字典。像这样:

format1 = workbook.add_format({'num_format': '#,###', 'align': 'center'})