简体   繁体   English

xlsxwriter创建的损坏的xlsx文件

[英]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.

相关问题 使用xlsxwriter编写.xlsx文件时出现UnicodeDecodeError错误 - UnicodeDecodeError error writing .xlsx file using xlsxwriter 如何导出用xlsxwriter编写的xlsx文件 - How to export a xlsx file written with xlsxwriter 如何使用 xlsxwriter 将数据写入 xlsx 文件? - How data write in xlsx file using xlsxwriter? 检测损坏的xlsx文件(zip zith更改扩展名)大熊猫 - Detect corrupt xlsx file (zip zith changed extension) pandas 使用xlsx writer进行条件格式化会生成带有“ text:contains”的损坏文件 - Conditional formatting with xlsx writer generates corrupt file with “text:containing” 使用Python和XlsxWriter导出到xlsx - Exporting to xlsx using Python and XlsxWriter 使用 django 上的 xlsxwriter 创建的 excel 文件上的 Remove.xls 扩展名 - Remove .xls extension on excel file created with xlsxwriter on django 如何忽略使用 python xlsxwriter 生成的 .xlsx 文件中显示的“公式不一致”警告? - How to Ignore "Inconsistent Formula" warning showing in generated .xlsx file using the python xlsxwriter? Python,将.csv转换为.xlsx时使用文件更正,而没有xlsxwriter.workbook模块 - Python, file corruoption when converting .csv to .xlsx, without xlsxwriter.workbook module xlsxwriter和xlrd问题-xlsx文件在另一台计算机上另存为ZIP,在另一台计算机上另存为前4个字节 - xlsxwriter & xlrd issue - xlsx file saved as ZIP on one machine, but with other first 4 bytes on another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM