将 SVG/PDF 转换为 EMF



我正在寻找一种将 matplotlib 图形保存为 EMF 文件的方法。Matplotlib允许我保存为PDF或SVG矢量文件,但不能保存为EMF。

经过长时间的搜索,我似乎仍然找不到使用 python 执行此操作的方法。希望任何人都有想法。

我的解决方法是使用子进程调用 inkscape,但这远非理想,因为我想避免使用外部程序。

我正在使用wx后端运行python 2.7.5和matplotlib 1.3.0。

  • 对于仍然需要这个的人,我写了一个基本函数,只要你安装了 inkscape,就可以让你从 matplotlib 将文件保存为 emf。

我知道操作不想要 inkscape,但后来找到这篇文章的人只是想让它工作。

import matplotlib.pyplot as plt
import subprocess
import os
inkscapePath = r"pathtoinkscape.exe"
savePath= r"pathtoimagesfolder"
def exportEmf(savePath, plotName, fig=None, keepSVG=False):
    """Save a figure as an emf file
    Parameters
    ----------
    savePath : str, the path to the directory you want the image saved in
    plotName : str, the name of the image 
    fig : matplotlib figure, (optional, default uses gca)
    keepSVG : bool, whether to keep the interim svg file
    """
    figFolder = savePath + r"{}.{}"
    svgFile = figFolder.format(plotName,"svg")
    emfFile = figFolder.format(plotName,"emf")
    if fig:
        use=fig
    else:
        use=plt
    use.savefig(svgFile)
    subprocess.run([inkscapePath, svgFile, '-M', emfFile])
 
    if not keepSVG:
        os.system('del "{}"'.format(svgFile))

#Example 用法

import numpy as np
tt = np.linspace(0, 2*3.14159)
plt.plot(tt, np.sin(tt))
exportEmf(r"C:UsersuserName", 'FileName')
我认为

这个函数很酷,但 inkscape 语法似乎在我的情况下不起作用。我在其他帖子中搜索并发现它为:

inkscape filename.svg --export-filename filename.emf 

因此,如果我在子流程参数中用--export-filename替换-M,一切正常。

最新更新