I have a large script generating Excel reports. I have lots of formats to make these reports look nice. To keep my code clean, I have one master function that makes the work book and creates the sheets, but then sends those sheets to other functions to create the report. Here's the pseudo code:
FORMATS = {dictionary of styles}
def add_styles(book):
s={}
for key in FORMATS:
s[key] = book.add_format(FORMATS[key])
return s
def generate(stuff):
book = XlsxWriter.Workbook(path)
styles = add_styles(book)
historysheet = book.add_sheet('History')
add_rate_history(historysheet, stuff, styles)
def add_rate_history(sheet, styles):
... write rows and cells
This seems like a workaround, to me. As far as I've been able to tell, individual sheets, once created, do not have a reference to the book in which they are created, and the book has no way to access the styles once they are created.
I have 24 styles defined in my FORMATS dictionary, and chose this method to make it easier to access them during these inner functions.
I suspect this may be the way Excel works, but it seems more pythonic to have these references.
Thanks,
Josh