python - Python 中的 Inkscape 扩展

标签 python inkscape

对于我的项目工作,我使用 Inkscape 执行两项任务:

  1. 要调整绘图页面的大小(我用其他软件创建的)以完全适合绘图:文件 --> 文档属性 --> 根据内容调整页面大小...
  2. 将文件另存为 PDF

此任务相当简单,但对于大量绘图来说非常耗时。

我检查了 Inkscape 中的宏功能,但没有这样的东西。然而我发现 Inkscape 允许人们使用 Python 实现自己的扩展脚本。

如果你们有类似的经历,能否帮助我将上面列出的步骤作为 Inkscape 扩展来实现。

可能有用的链接:http://wiki.inkscape.org/wiki/index.php/PythonEffectTutorial

编辑:接受的答案无法使用内部 python 扩展解决我的请求,但它通过使用 inkscape 命令行选项解决了任务。

最佳答案

我从未在 inkscape 中编写过脚本,但我一直使用 python 中的 inkscape(通过 subprocess 模块)。如果您在命令行上输入 inkscape --help,您将看到所有选项。我相信对于您的用例,以下内容将会起作用:

inkscape -D -A myoutputfile.pdf  myinputfile.whatever

-A 表示输出为 PDF(需要文件名),-D 表示根据绘图调整大小。

如果您从未使用过 subprocess 模块,最简单的方法就是使用 subprocess.call,如下所示:

subprocess.call(['inkscape', '-D', '-A', outfn, inpfn])

编辑:

处理命令行上传递的输入文件名的最简单的脚本(未经测试!)看起来像这样:

import sys
import os
# Do all files except the program name
for inpfn in sys.argv[1:]:
    # Name result files 'resized_<oldname>.pdf'
    # and put them in current directory
    shortname = os.path.basename(inpfname).rsplit('.',1)[0]
    outfn = 'resized_%s.pdf' % shortname
    subprocess.call(['inkscape', '-D', '-A', outfn, inpfn])

关于python - Python 中的 Inkscape 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31841071/

相关文章:

python - Python中**arg的问题

python - numpy 数组或 pandas DataFrame 中的条件过滤

svg - Inkscape 导出到 Core 图形?

pdf - 将 PDF 文本转换为轮廓?

python - seaborn中轴标签的字体大小

python split 和 re.split 不捕获字符串中出现的制表符或空格

css - 在 SVG 动画中更改对象/形状的颜色

perl - 从 perl 使用 Inkscape shell

Python 3 : perform additional calculation inside a generator