xlsxwriter创建的损坏的xlsx文件

时间:2018-08-21 19:07:31

标签: python python-3.x python-3.6 xlsx 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()

在Excel中打开输出文件会出现此错误:

Excel cannot open the file 'rpt.xlsx' because the file format or file 
extention is not valid

当尝试将excel文件读回python(使用pandas.read_excel())时,我收到一条错误消息,指出File size 0 bytes

我似乎无法找到解决此问题的方法,并且花了很多天的时间来解决它。 任何帮助将不胜感激!

--------------------------- 编辑 ------------ -----------------------------

我尝试删除insert_image()add_table()语句,但发现工作表仍然损坏。

1 个答案:

答案 0 :(得分:0)

如果您可以添加一个可以正常运行的程序,则可以为您节省时间,并为其他人节省时间。

我已修改您的示例以包括一些示例数据,并将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()

输出文件未损坏,请参见下文,所以您遗漏的程序的某些部分可能会起作用?

enter image description here