writing to sheet1, sheet2, sheet1, etc

Issue #72 resolved
ccurvey created an issue

I'm not sure if this is a bug or if it is expected to work. I'm trying to take the results of a query and write them to different sheets based on some attribute of the rows. (A test program that tries to write out even and odd numbers to different sheets is attached.) When I try to save, I'm getting an AttributeError.

Here's the stack trace -- I'm having trouble formatting it correctly.

AttributeError: 'str' object has no attribute '_styles' File "c:\Users\ccurvey\augean\sheetplay.py", line 15, in <module> book.save(r"c:\temp\foobar.xlsx")

File "c:\Python26\Lib\site-packages\openpyxl-1.5.2-py2.6.egg\openpyxl\workbook.py", line 186, in save save_workbook(self, filename)

File "c:\Python26\Lib\site-packages\openpyxl-1.5.2-py2.6.egg\openpyxl\writer\excel.py", line 140, in save_workbook writer = ExcelWriter(workbook) File "c:\Python26\Lib\site-packages\openpyxl-1.5.2-py2.6.egg\openpyxl\writer\excel.py", line 53, in init self.style_writer = StyleWriter(self.workbook)

File "c:\Python26\Lib\site-packages\openpyxl-1.5.2-py2.6.egg\openpyxl\writer\styles.py", line 36, in init self._style_list = self._get_style_list(workbook)

File "c:\Python26\Lib\site-packages\openpyxl-1.5.2-py2.6.egg\openpyxl\writer\styles.py", line 43, in _get_style_list for style in worksheet._styles.values(): }}}

Comments (6)

  1. Maarten De Paepe

    hi ccurvey,

    Glad to see you're having some fun with openpyxl.

    Unfortunately, though, it seems your script isn't fully correct.

    book.add_sheet("Even")
    even = book.get_active_sheet()
    

    With the code above, you are adding the String "Even" to the list of worksheets in the workbook. The reason why you are getting an error is because, at the end of your script, you are trying to save a String, instead of a Worksheet.

    Also note that creating or adding a sheet, does not automatically set it as the active sheet.

    This code will work better :) Just replace the above code with this one. For more information, please consult the documentation: http://packages.python.org/openpyxl/index.html

    even = book.create_sheet()
    even.title = "Even"
    

    Thanks to you, though, we now know that a check was forgotten when adding a sheet to a workbook. A patch will be written to only allow adding a worksheet to a workbook when using add_sheet().

    Thanks :)

    Regards

  2. Log in to comment