Reputation: 582
I have the following code to create an .xlsx file with xlsxwriter
:
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 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
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.
Upvotes: 2
Views: 3771
Reputation: 41644
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:
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?
Upvotes: 2