Reputation: 175
I am using the following code to create and excel file using xlsxwriter and openpyxl to edit it as I may need to read from other excel files later down the line, however when I try to open the file it gives me an error that the excel file is corrupt or the extension is incorrect. When the source file is saved as .xlsm this error is not present, I would like to know why that is.
import xlsxwriter
import openpyxl
# Create a workbook and add a worksheet.
workbook = xlsxwriter.Workbook('Dabble dabble.xlsx')
worksheet = workbook.add_worksheet()
workbook.close()
target_file = 'Dabble dabble.xlsx'
i = 2
No = i - 1
Company = "Panasonic"
Location = "California"
Store_type = "Hyper Market"
Date = "1/1/2020"
No_loc = "A" + str(i)
company_loc = "C" + str(i)
location_loc = "B" + str(i)
store_type_loc = "D" + str(i)
date_loc = "E" + str(i)
srcfile = openpyxl.load_workbook(target_file, read_only=False,
keep_vba=True)
sheetname = srcfile['Sheet1']
sheetname[No_loc] = No
sheetname[company_loc] = Company
sheetname[location_loc] = Location
sheetname[store_type_loc] = Store_type
sheetname[date_loc] = Date
# Table headers
sheetname["B1"] = "Location"
sheetname["C1"] = "Company"
sheetname["D1"] = "Store Type"
sheetname["E1"] = "Date"
i = i + 1
srcfile.save(target_file) #Saving data to file
import pandas as pd
target_file = "Dabble dabble.xlsx"
df= pd.read_excel(target_file)
print (df)
However, when I parse the file using pandas it reads the data, which shows me that the file was created and written.
Unnamed: 0 Location Company Store Type Date
0 1 California Panasonic Hyper Market 1/1/2020
Upvotes: 3
Views: 1726
Reputation: 41524
The issue is that you are setting keep_vba=True
but the file you are dealing with isn't a xlsm file and doesn't have a vbaProject
file. Just set it to false or omit the option.
srcfile = openpyxl.load_workbook(target_file,
read_only=False,
keep_vba=False)
Upvotes: 2