Search code examples
imagepython-2.7python-imaging-libraryxlsxwriterstringio

Write png to xlsx file with XlsxWriter in python?


Following the guide for XlsxWriter here: Docs I have tried using Pillow to get the png file. Then write to the worksheet using the guide from the link above. I tried to use StringIO.

    f = Image.open('/opt/folder/' + 'cc.png')
    output = StringIO.StringIO(f)
    f.save(output)
    f = output.getvalue()
    output.close()
    frontSheet.insert_image('B1', f, {'x_scale': 0.5, 'y_scale': 0.5})

The error message said that NoneType object is not callablePerforming.

    cc = Image.open('/opt/folder/' + 'cc.png')
    f = cStringIO.StringIO(Image.open('/opt/folder/' + 'cc.png'))
    cc.save(im2, 'PNG')
    frontSheet.insert_image('B1', cc, {'x_scale': 0.5, 'y_scale': 0.5}

The error message said that it cannot identify the image file. How do I write the png file to the worksheet?


Solution

  • You can just insert the image directly without Pillow:

    frontSheet.insert_image('B1', 
                            '/opt/folder/cc.png', 
                            {'x_scale': 0.5, 'y_scale': 0.5})