Python 中的 Inkscape 扩展

Inkscape extension in Python

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

  1. 要调整绘图(我用其他软件创建的)的页面大小以完全适合绘图:File --> Document Properties --> Resize page to content...
  2. 将文件另存为 PDF

这个任务比较简单,但是绘图量大的话比较费时间

我检查了 Inkscape 中的宏功能,但没有这样的东西。但是我发现 Inkscape 允许使用 Python.

实现自己的扩展脚本

如果你们中的任何人有类似的经历,你能帮我实现上面列出的步骤作为 Inkscape 扩展吗?

可能有用 link: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])