问题
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.
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).
The code is somewhat simple:
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))
The error i get is:
IOError: cannot find loader for this WMF file
These Word documents come from average user that may just have cut-and-paste images from the web or insert from file.
Is there a solution for me on a linux system?
Thanks.
EDIT:
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?
回答1:
pip install Pillow
from PIL import Image
Image.open("xxx.wmf").save("xxx.png")
回答2:
You need to understand what you are dealing with in order to see why what you are attempting to do is problematic. WMF files (or the more recent EMF and EMF+ formats) require Windows GDI to render the image it describes. So there is no simple solution when you are converting this format outside of Windows, since you need to replicate the GDI API.
One solution is to use the unoconv tool which relies on the UNO bindings for OpenOffice/LibreOffice. A second solution would use the pyemf module to decode the input, and then a second tool (to be done by you) would render it.
回答3:
I found it easier to use the Wand package for such conversion. I tried the previous suggestions without success. So here is what I did: (BTW, I wanted to convert all '.wmf' files into pdf)
import os
from wand.image import Image as wima
folder='C:/Users/PythonLover/Pictures/pics'
for oldfilename in os.listdir(folder):
if oldfilename.endswith(".wmf"):
with wima(filename=folder+'/'+oldfilename) as img:
newfilename = oldfilename.split('.')[0]+'.pdf'
newfilename = folder+'/'+newfilename
img.format = 'pdf'
img.save(filename=newfilename)
回答4:
You may use libwmf
to convert image to SVG and then pyrsvg
to convert to PNG (described in another question).
I haven't found libwmf
project website, but Debian (and Ubuntu) has package libwmf-bin
that contains wmf2svg
utility.
来源:https://stackoverflow.com/questions/14103891/convert-emf-wmf-files-to-png-jpg