I’m using Pandas and Xlsxwriter to write a spreadsheet to a .XLSX file. Here’s my incomplete code.
'''datadict = has all my data with keys.
options = a class holding different data to pass between functions.
options.headersxl = ['Hdr1', 'hdr2', 'hdr3', 'hdr4']
'''
import pandas
import xlsxwriter
procname = str(inspect.stack()[0][3]) + ":"
fn = os.path.join(options.progdir, options.datafn + ".xlsx")
outdata = []
sheetname = 'Sheet1'
outdata.append(options.headersxl) # Column headers as a list.
for key, val in datadict.items():
# tlist = [key.split(',') for key in datadict] # Split one row
t = datadict[key]
tlist = t.split(',') # Convert one row to a list.
outdata.append(tlist) # Save one row
df=pandas.DataFrame(outdata) # Make a dataframe from the list of lists.
(numrows, numcols) = df.shape
try:
writer = pandas.ExcelWriter(fn, engine='xlsxwriter', date_format='MM/DD/YYYY')
except PermissionError as e:
print(f"{procname} ERROR: Please close the spreadsheet before running this.")
sys.exit()
except Exception as e:
sys.exit(f"{procname} ERROR on pandas.ExcelWriter: {e}")
try:
df.to_excel(writer, sheet_name=sheetname, header=options.headersxl, index=False, freeze_panes=(1,0) ) # Save to file.
except Exception as e:
sys.exit(f"{procname} ERROR on to_excel: {e}")
# strings_to_numbers = convert all strings to floats/numbers where possible.
# wbook = writer.book(fn, {'strings_to_numbers': True, 'default_date_format': 'dd/mm/yy'})
workbook = writer.book
worksheet = writer.sheets[sheetname]
hdrformat = workbook.add_format({'bold':True, 'text_wrap':True, 'valign':'top', 'align':'left'})
dlrformat = workbook.add_format({'num_format': '0.00', 'align':'right'})
dateformat = workbook.add_format({'num_format': 'mm/dd/yy'})
writer.sheets[sheetname].set_column('A:A', 25)
writer.sheets[sheetname].set_column('B:B', 15, dateformat)
writer.sheets[sheetname].set_column('D:E', 25)
writer.sheets[sheetname].set_column('F:G', 12, dlrformat)
writer.sheets[sheetname].set_column('J:K', 12, dlrformat)
In my Excel file the first two rows look like this:
0 1 2 3 4...
Hdr1 hdr2 hdr3 hdr4 ...
I want to get rid of the first row which says “0 1 2 3 4”.
If I add my options.headersxl as the first row of the dataframe I still get the first row as numbers even if I do this:
df.to_excel(writer, sheet_name=sheetname, header=False, index=False, freeze_panes=(1,0) ) # Save to file.
Any idea where I’m going wrong here?
I’ve read the docs on xlsxwriter and pandas .to_excel about headers and I still don’t see what I’m doing wrong. Examples on the internet often don’t work either. Some of the code above is from internet examples so it’s possible it’s not correct, in the wrong order, etc.
Thank you.
EDIT: My data is row-based, that is, it is constructed row-by-row, as if I were writing one row at a time to a .CSV file or text file.