【问题标题】:Inkscape extension in PythonPython 中的 Inkscape 扩展
【发布时间】:2015-08-05 19:21:14
【问题描述】:

对于我的项目工作,我将 Inkscape 用于两个任务:

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

这项任务相当简单,但是对于大量图纸来说却很耗时。

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

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

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

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

【问题讨论】:

    标签: python inkscape


    【解决方案1】:

    我从未在inkscape 中编写脚本,但我一直使用python 中的inkscape(通过子进程模块)。如果你在命令行输入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])
    

    【讨论】:

    • 你的意思是通过命令行传递?还是来自文件?还是要转换一个目录下的所有文件?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多