【问题标题】:Write png to xlsx file with XlsxWriter in python?在 python 中使用 XlsxWriter 将 png 写入 xlsx 文件?
【发布时间】:2017-02-05 08:53:12
【问题描述】:

在此处遵循 XlsxWriter 指南: Docs 我尝试使用 Pillow 来获取 png 文件。然后使用上面链接中的指南写入工作表。我尝试使用 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})

错误信息说 NoneType 对象是不可调用的。

    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}

错误消息说它无法识别图像文件。如何将 png 文件写入工作表?

【问题讨论】:

  • 有什么建议吗? @jmcnamara

标签: image python-2.7 pillow xlsxwriter stringio


【解决方案1】:

你可以直接插入图片,不用枕头:

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

【讨论】:

  • 这给了我一个错误,说“无法识别图像文件”@jmcnamara
  • 那么它可能不是一个有效的 png/jpeg/bmp 文件。当你在应用程序中打开它时会发生什么?
  • 它在 Windows 照片查看器中正常打开,因为它是一个 png 图像文件@jmcnamara
  • 在这种情况下open an issue on Github 并将图像附加到它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-30
  • 2016-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-28
  • 2015-02-21
相关资源
最近更新 更多