问题描述
我收到一个带有 Word docx 文档的表单上传.我成功完成了所有解析.然后我必须在网络上显示该 Word 文档.
I am receiving an form upload with a Word docx document. I got all the parsing done successfully. I have to then display that Word document on the web.
我此时遇到的问题是我嵌入了 EMF 文件(PIL 库将其识别为 WMF 格式),我无法弄清楚如何将它们转换为可以在网络上显示的内容(任意选择)PNG).
The problem I am running into at this moment is that I have embedded EMF files (that the PIL library recognizes as WMF format), and I cannot figure how to convert them to something that can be displayed on the web (arbitrarily chosen PNG).
代码有点简单:
im = PIL.Image.open(StringIO.StringIO(data))
fmt = im.format
if (fmt == 'WMF'):
fmt = 'PNG'
output = StringIO.StringIO()
im.save(output, format=fmt)
data = output.getvalue()
output.close()
return '''<img src="data:image/{0};base64,{1}" />'''.format(fmt, base64.encodestring(data))
我得到的错误是:
IOError: cannot find loader for this WMF file
这些 Word 文档来自普通用户,他们可能只是从网络上剪切和粘贴图像或从文件中插入.
These Word documents come from average user that may just have cut-and-paste images from the web or insert from file.
在 linux 系统上有没有适合我的解决方案?
Is there a solution for me on a linux system?
谢谢.
为了我的辩护,我尝试将该文档上传到谷歌驱动器,但图像也未显示.也许没有简单的解决方案?
To my defense, I tried to upload that document to google drive and the image is not displayed either. Maybe there are no simple solutions?
推荐答案
pip install Pillow
pip install Pillow
从 PIL 导入图像
Image.open("xxx.wmf").save("xxx.png")
Image.open("xxx.wmf").save("xxx.png")
这篇关于将 EMF/WMF 文件转换为 PNG/JPG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!