Kristof Pal
Kristof Pal

Reputation: 1026

Inkscape extension in Python

For my project work, I am using Inkscape for two tasks:

  1. To resize the page of a drawing (which I have created with another software) to fit exactly the drawing: File --> Document Properties --> Resize page to content...
  2. Save the file as PDF

This tasks are rather simple, however they are time-consuming for larger amount of drawings.

I checked for macros functionality in Inkscape but there is no such thing. However I found out that Inkscape allows one to implement his own extension scripts using Python.

If any of you has similar experience, could you help me implement the steps listed above as an Inkscape extension.

Potentially useful link: http://wiki.inkscape.org/wiki/index.php/PythonEffectTutorial

EDIT: the accepted answer does not solve my request using internal python extension, but it solves the task by using the inkscape command-line options.

Upvotes: 2

Views: 4684

Answers (1)

Patrick Maupin
Patrick Maupin

Reputation: 8137

I have never scripted stuff from inside inkscape, but I use inkscape from python all the time (via the subprocess module). If you type inkscape --help on the command line, you will see all the options. I believe for your use-case, the following will work:

inkscape -D -A myoutputfile.pdf  myinputfile.whatever

The -A says to output to PDF (requires the filename), and the -D tells it to resize to the drawing.

If you've never used the subprocess module, the easiest thing would be to use subprocess.call like this:

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

EDIT:

The cheesiest possible script (untested!) to handle input filenames passed on the command line would look something like this:

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

Upvotes: 2

Related Questions