[英]Corrupt xlsx file created by xlsxwriter
I have the following code to create an .xlsx file with xlsxwriter
: 我有以下代码使用xlsxwriter
创建.xlsx文件:
workbook = xlsxwriter.Workbook(file_path)
workbook.encoding='utf-8'
df_report = pd.DataFrame(columns=['name', 'other', 'days', 'id', 'type',
'call_type', 'role', 'breach'])
df_report = pd.concat([df_report, rpt1[rpt1['name'] == clientName]])
df_report.sort_values(by = ['breach'], inplace = True, ascending = False)
df_report = df_report[['other', 'days', 'id', 'role','breach']]
worksheet = workbook.add_worksheet('RPT_001_Q')
worksheet.insert_image('A1', 'logo.png', {'x_scale': 0.25, 'y_scale': 0.25})
merge_format = workbook.add_format({
'bold': 1,
'border': 2,
'align': 'center',
'valign': 'vcenter',
'fg_color': 'white'})
worksheet.merge_range('A1:C5', '', merge_format)
merge_format.set_border_color('green')
bold = workbook.add_format({'bold': 1})
worksheet.write('A6', 'Report Date:', bold)
worksheet.write('B6', self.date, bold)
worksheet.write('A7', 'Report Name:', bold)
worksheet.write('B7', 'RPT_001_Q ', bold)
worksheet.write('A8', 'Begin Date:', bold)
worksheet.write('B8', start, bold)
worksheet.write('A9', 'End Date:', bold)
worksheet.write('B9', end, bold)
worksheet.write('A10', 'Entity:', bold)
worksheet.write('B10', client, bold)
worksheet.write('A11', 'Type:', bold)
worksheet.write('B11', 'Type A', bold)
worksheet.write('A12', 'Call Type:', bold)
worksheet.write('B12', 'Type D', bold)
if len(df_report) == 0:
worksheet.write('A13', 'No Data found for this Report in the time constraint given', bold)
else:
table = 'A13:E' + str(len(df_report) + 13)
data = df_report.values.tolist()
worksheet.add_table(table, {'data' : data, 'style': 'Table Style Medium 1', 'columns' : [
{'header' : 'other'},
{'header' : 'days'},
{'header' : 'id'},
{'header' : 'role'},
{'header':'breach'},]})
workbook.close()
The output file gives this error upon opening in Excel: 在Excel中打开时,输出文件会给出此错误:
Excel cannot open the file 'rpt.xlsx' because the file format or file
extention is not valid
When trying to read the excel file back into python (using pandas.read_excel()
), I receive an error stating File size 0 bytes
尝试将excel文件读回python(使用pandas.read_excel()
)时,出现错误,指出File size 0 bytes
I cannot seem to figure out a way to fix this, and have spend a good number of days trying to solve it. 我似乎无法找出解决此问题的方法,并且花了很多天的时间来解决它。 ANY help would be greatly appreciated! 任何帮助将不胜感激!
--------------------------- EDIT ----------------------------------------- --------------------------- 编辑 ---------------------- -------------------
I tried removing the insert_image()
and add_table()
statements, but found the worksheet was still corrupt. 我尝试删除insert_image()
和add_table()
语句,但是发现工作表仍然损坏。
It saves time for you, and the people answering, if you can add a fully working program. 如果您可以添加一个可以正常运行的程序,则可以为您和其他人节省时间。
I've modified your example to include some sample data and turned some of the variable like self.date
into strings: 我已经修改了您的示例,以包含一些示例数据,并将诸如self.date
之类的某些变量转换为字符串:
import pandas as pd
import xlsxwriter
file_path = 'test.xlsx'
workbook = xlsxwriter.Workbook(file_path)
data_list = [10, 20, 30, 20, 15]
df_report = pd.DataFrame({'other': data_list,
'days': data_list,
'id': data_list,
'role': data_list,
'breach': data_list})
worksheet = workbook.add_worksheet('RPT_001_Q')
worksheet.insert_image('A1', 'logo.png', {'x_scale': 0.9, 'y_scale': 0.9})
merge_format = workbook.add_format({
'bold': 1,
'border': 2,
'align': 'center',
'valign': 'vcenter',
'fg_color': 'white'})
worksheet.merge_range('A1:C5', '', merge_format)
merge_format.set_border_color('green')
bold = workbook.add_format({'bold': 1})
worksheet.write('A6', 'Report Date:', bold)
worksheet.write('B6', 'self.date', bold)
worksheet.write('A7', 'Report Name:', bold)
worksheet.write('B7', 'RPT_001_Q ', bold)
worksheet.write('A8', 'Begin Date:', bold)
worksheet.write('B8', 'start', bold)
worksheet.write('A9', 'End Date:', bold)
worksheet.write('B9', 'end', bold)
worksheet.write('A10', 'Entity:', bold)
worksheet.write('B10', 'client', bold)
worksheet.write('A11', 'Type:', bold)
worksheet.write('B11', 'Type A', bold)
worksheet.write('A12', 'Call Type:', bold)
worksheet.write('B12', 'Type D', bold)
if len(df_report) == 0:
worksheet.write('A13', 'No Data found for this Report in the time constraint given', bold)
else:
table = 'A13:E' + str(len(df_report) + 13)
data = df_report.values.tolist()
worksheet.add_table(table, {'data' : data, 'style': 'Table Style Medium 1', 'columns' : [
{'header' : 'other'},
{'header' : 'days'},
{'header' : 'id'},
{'header' : 'role'},
{'header':'breach'},]})
workbook.close()
The output file isn't corrupted, see below, so is there some part of the program that you left out that might be having an effect? 输出文件未损坏,请参见下文,因此您遗漏的程序某些部分可能会起作用吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.