转换SVG/PDF到EMF [英] Convert SVG/PDF to EMF

查看:782
本文介绍了转换SVG/PDF到EMF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种将matplotlib图形另存为EMF文件的方法. Matplotlib使我可以另存为PDF或SVG矢量文件,但不能另存为EMF.

I am looking for a way to save a matplotlib figure as an EMF file. Matplotlib allows me to save as either a PDF or SVG vector file but not as EMF.

经过长时间的搜索,我似乎仍然找不到使用python进行此操作的方法.希望任何人都有一个主意.

After a long search I still cannot seem to find a way to do this with python. Hopefully anyone has an idea.

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

My workaround is to call inkscape using subprocess but this is far from ideal as I would like to avoid the use of external programs.

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

I'm running python 2.7.5 and matplotlib 1.3.0 using the wx backend.

推荐答案

对于仍然需要此功能的任何人,我编写了一个基本函数,只要您具有 inkscape 已安装.

我知道操作人员不希望出现墨迹混乱,但是后来找到此帖子的人只是想使其发挥作用.

For anyone who still needs this, I wrote a basic function that can let you save a file as an emf from matplotlib, as long as you have inkscape installed.

I know the op didn't want inkscape, but people who find this post later just want to make it work.

import matplotlib.pyplot as plt
import subprocess
import os
inkscapePath = r"path\to\inkscape.exe"
savePath= r"path\to\images\folder"

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))

用法示例

import numpy as np
tt = np.linspace(0, 2*3.14159)
plt.plot(tt, np.sin(tt))
exportEmf(r"C:\Users\userName", 'FileName')

这篇关于转换SVG/PDF到EMF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆