I have Python 3.11.9 and Pandas 2.2.2 on Windows 10.
I’m creating an Excel spreadsheet with a header row which will be specified separately. My program has 2 parts: gather data and put it all in a list called outarr
. The second subroutine actually writes out the spreadsheet using the data in outarr
.
In my data in one column “D” sometimes the data says “(missing)”. How do I color this text foreground red if the text in column D is “(missing)”)?
The initial parts of the subroutine writeexcel
, which actually writes out outarr
to an Excel file look like this:
try:
df=pandas.DataFrame(outdata, columns=options.headersxl) # Make a dataframe from the list of lists.
df = df.sort_values(['Job num']) # Sort by these columns.
except ValueError as e:
sys.exit(f"{procname} ERROR-dataframe1: Num headers does not match num columns.")
except Exception as e:
sys.exit(f"{procname} ERROR-dataframe2: {e}")
(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}")
Thank you!