XlsxWriter Feature Request: Format Access

37 views
Skip to first unread message

Josh English

unread,
May 21, 2013, 12:35:38 PM5/21/13
to python...@googlegroups.com
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

John McNamara

unread,
May 21, 2013, 1:07:47 PM5/21/13
to python...@googlegroups.com


On Tuesday, May 21, 2013 5:35:38 PM UTC+1, Josh English wrote:

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. 

Hi Josh,

Some of this is due to the Excel structure bleeding through to the interface and some is due to implementation details. Worksheets don't have a reference to the Workbook since that would create circular references (it would be possible to weaken the references but that exposes some other potential issues). Workbooks do have access to formats but they aren't exposed in the API.

Your "workaround" seems fine to me. I appreciated that it is probably a workaround for a workaround since the ability to format areas of a worksheet separately from the data is missing and is the subject of another feature request:


    https://github.com/jmcnamara/excel-writer-xlsx/issues/22

John



 
Reply all
Reply to author
Forward
0 new messages