Convert EMF/WMF files to PNG/JPG

后端 未结 4 825
深忆病人
深忆病人 2021-02-14 10:23

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

相关标签:
4条回答
  • 2021-02-14 10:27

    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.

    0 讨论(0)
  • 2021-02-14 10:38

    pip install Pillow

    from PIL import Image

    Image.open("xxx.wmf").save("xxx.png")

    0 讨论(0)
  • 2021-02-14 10:41

    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.

    0 讨论(0)
  • 2021-02-14 10:45

    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)
    
    0 讨论(0)
提交回复
热议问题