Inserting image fails with Python 3.3 / Pillow 2.0

Issue #191 resolved
Former user created an issue

Running the logo example from the docs fails on Python 3.3

C:\>py -2 logo.py

C:\>py -3 logo.py
Traceback (most recent call last):
  File "logo.py", line 20, in <module>
    wb.save('logo.xlsx')
  File "C:\Python33\lib\site-packages\openpyxl-1.6.1-py3.3.egg\openpyxl\workbook.py", line 214, in save
    save_workbook(self, filename)
  File "C:\Python33\lib\site-packages\openpyxl-1.6.1-py3.3.egg\openpyxl\writer\excel.py", line 155, in save_workbook
    writer.save(filename)
  File "C:\Python33\lib\site-packages\openpyxl-1.6.1-py3.3.egg\openpyxl\writer\excel.py", line 138, in save
    self.write_data(archive)
  File "C:\Python33\lib\site-packages\openpyxl-1.6.1-py3.3.egg\openpyxl\writer\excel.py", line 79, in write_data
    self._write_worksheets(archive, shared_string_table, self.style_writer)
  File "C:\Python33\lib\site-packages\openpyxl-1.6.1-py3.3.egg\openpyxl\writer\excel.py", line 131, in _write_worksheets
    img.image.save(buf, format= 'PNG')
  File "C:\Python33\lib\site-packages\PIL\Image.py", line 1467, in save
    save_handler(self, fp, filename)
  File "C:\Python33\lib\site-packages\PIL\PngImagePlugin.py", line 538, in _save
    fp.write(_MAGIC)
TypeError: string argument expected, got 'bytes'

Using

  • Python 3.3.1rc1 32-bit
  • Pillow 2.0.0
  • openpyxl fc6e9662906b
  • Windows 7 64-bit

Changing to BytesIO fixes the problem:

diff -r fc6e9662906b -r 8a4d021dfebd openpyxl/writer/excel.py
--- a/openpyxl/writer/excel.py  Fri Mar 08 17:43:51 2013 +0100
+++ b/openpyxl/writer/excel.py  Tue Apr 02 14:04:30 2013 +0200
@@ -127,7 +127,7 @@
                     chart_id += 1

                 for img in sheet._images:
-                    buf = StringIO()
+                    buf = BytesIO()
                     img.image.save(buf, format= 'PNG')
                     archive.writestr(PACKAGE_IMAGES + '/image%d.png' % image_id, buf.getvalue())
                     image_id += 1

Comments (2)

  1. Log in to comment