Inkscape.org
Beginners' Questions Batch conversion of PDF to EPS via command line
  1. #1
    Fredm Eisenhauer Fredm Eisenhauer @fredm.eisenhauer

    I am trying to convert some figures from *.pdf to *.eps via Inkscape and Python. I resorted to the https://inkscape.org/doc/inkscape-man.htmlData Visualization and Inkscape Wiki and came up with the code below. However when I run the script, no output is written into the working directory.

    import os 
    import subprocess
    
    # Change to working directory
    os.chdir("C:/Users/Username/Figures")
    
    # Iterate over all PDF figures
    for figure in [i for i in os.listdir() if i[-4:]=='.pdf']:
    
        subprocess.run([
            "C:/Program Files/Inkscape/inkscape.exe",
            figure, # Input figure
            f"--export-filename={figure[:-4]}.eps", # Output figure
            "--batch-process"
            ])
    

    There is also a similar question on Stackoverflow. However, if I replace *.emf with *.eps in the answer the files cannot be read by LaTeX.

    Update: Inkscape version 0.92.4 (5da689c313, 2019-01-14)

  2. #2
    inklinea inklinea @inklinea⛰️

    I would try this from the command line first 

    inkscape --export-filename=test.eps ./testfile.pdf

    to make sure that the conversion works, before trying to use a subprocess in python

    I haven't used 0.9x for a while.

    However for Inkscape 1.1+ it should not be necessary to use python at all:

    inkscape --export-type=eps *.pdf

    will convert all pdf in the folder to eps

    The new eps will have the same filename as the input pdf
     

  3. #3
    Victor Westmann Victor Westmann @victorwestmann
    *

    Is there any place where we can lean how to manipulate Inkscape from the command line?

    Also, if possible, is there some sort of sandbox for people who are trying to develop an inkscape extension?
    (to write python code to directly manipulate SVG paths on Inkscape screen?)

  4. #4
    inklinea inklinea @inklinea⛰️

    https://inkscape.gitlab.io/extensions/documentation/authors/index.html

    https://wiki.inkscape.org/wiki/index.php/Using_the_Command_Line

    The command line is changing from 1.1 - 1.2. 

    At the moment in Inkscape 1.1+ there are actions and verbs. The difference is that verbs require the gui to be triggered - and that reduces their speed. The also cannot be used in 'shell' mode which is a faster way of using the command line without ending the Inkscape instance.

    Verbs require either --batch-mode or --with-gui in the command line - actions do not.

    In 1.2 there are just actions.

    I wrote this a while ago for Inkscape 1.1 - https://www.raincloud.co.uk/svg/ibc/ibc.html

    What do you mean by a sandbox ? 

     

  5. #5
    Victor Westmann Victor Westmann @victorwestmann

    Thank you for the links. they're super helfpul! By sandbox I mean... if there's a place where we can write commnads and Inkscape would draw the SVG/paths we want to see on screen.
    The reason for this is that I'm struggling to write an extension to do a simple timeline. There are a lot of things to be drawned on screen and I'm still learning inkex and how to write the .py portion of it.

  6. #6
    inklinea inklinea @inklinea⛰️

    No not as such.

    I am a hobby programmer, so I am a basic python programmer.

    I use IntelliJ IDEA community edition to write. The reason being I prefer it to pycharm - as it allows me to edit other files with autocomplete and closure checking such as .inx - if you define them as xml.

    In Inkscape I assign a shortcut to whichever extension I am writing, extensions appear under Edit>Preferences>Keyboard>Extensions.

    The name is defined by the id in the .inx file - for example <id>org.inkscape.effect.param_curves</id>

    If you just want  to run the extension (with previous settings) it would be org.inkscape.effect.param_curves.noprefs

    or with the dialogue ( if the extension has one ) org.inkscape.effect.param_curves

    This also applies to the command line - the extension can be called with org.inkscape.effect.param_curves.noprefs ( will use last parameters when inkscape was cleany exited )

    or org.inkscape.effect.param_curves to trigger the gui dialogue if the extension has a dialogue

    It is no possible to pass parameters from the command line.

    In inkscape 1.1 all extensions are verbs.

  7. #7
    matej.kaloc matej.kaloc @matej.kaloc

    Hello, I have a question (probably to @inklinea): I'm newbie in inkscape and trying to bulk export (and also rotate objects) SVG files to EMF, unfortunately the python script gives me an error when running subprocess.run, so I looked for other alternatives and found your advice on using Inkscape via command line without Python script - in my case I used:

    inkscape --export-type=emf *.svg

    However, even though I try it in different versions starting with 1.1, (tried in versions: Inkscape 1.1, Inkscape 1.1.1, Inkscape 1.1.2, Inkscape 1.2.1) so in all of them inkscape takes the expression *.svg as a direct file name, which of course it doesn't find afterwards, so I received the following error message:

    ** (org.inkscape.Inkscape:5004): WARNING **: 22:47:41.447: Can't open file: C:\Documents\CETIN\new\*.svg (doesn't exist)
    ** (org.inkscape.Inkscape:5004): WARNING **: 22:47:41.456: Can't open file: C:\Documents\CETIN\new\*.svg (doesn't exist)
    ink_file_open: 'C:\Documents\CETIN\new\*.svg' cannot be opened!
    InkscapeApplication::document_open: Failed to open: C:\Documents\CETIN\new\*.svg
    ConcreteInkscapeApplication::on_open: failed to create document!

    Do you have any idea, where the problem could be? Thanks for any idea!

     

  8. #8
    inklinea inklinea @inklinea⛰️

    For windows and Ubuntu the following should work.

    Inkscape 1.2.1

    Just remember to be aware of any existing files which will be overwritten.

    inkscape --actions="export-type:emf;export-do" *.svg
     

  9. #9
    inklinea inklinea @inklinea⛰️

    After a question in chat, it seems that Inkscape / Windows interprets *.svg as a literal string instead of a wildcard.

    If you are happy to use powershell the following should work:

    $files = Get-ChildItem -Name ./*.svg
     for ($i=0; $i -lt $files.Count; $i++) {
     
     inkscape --actions="export-type:emf;export-do" $files[$i]
     
     }

    -------------------
    save the above code as inkLoop.ps1

    then copy the file to the folder you have the .svg files in and run:

    powershell -ExecutionPolicy Bypass -File .\inkLoop.ps1

    presuming inkscape is already in you path

  10. #10
    matej.kaloc matej.kaloc @matej.kaloc

    I tried it and it works nice! Even with the flip extension, thank you very much for the advice and help!!

  11. #11
    Jacek Pardyak Jacek Pardyak @jacek.pardyak
    *

    Based on @inklinea comment and for consistency:

    In Python:

    import subprocess, glob 
    files = glob.glob('./PSL/' + '*.svg', recursive=True)
    for file in files:
        args = ['inkscape', '--actions=export-type:emf;export-do', file]
        p = subprocess.Popen(args)
    

    In R:

    files = list.files("./PSL/", pattern = ".svg", full.names = T)
    for (file in files) {
      fmt = 'inkscape --actions="export-type:emf;export-do" %s'
      command <- sprintf(fmt, file)
      system(command, intern = TRUE)
    }
    

    In Powershell​​​​​​​ (Windows 10):

    $files = Get-ChildItem -Name ./PSL/*.svg
    cd ./PSL
    for ($i=0; $i -lt $files.Count; $i++) {
      inkscape --actions="export-type:emf;export-do" $files[$i]
    }
    

    In Bash​​​​​​​ (Ubuntu 22.04):

    #!/bin/bash
    files="./PSL/*.svg"
    for file in $files
    do
      inkscape --actions="export-type:emf;export-do" $file
    done
    

    `/PSL` is the directory with SVG files. Python code seems to run the fastest among Powershell, Bash, Python and R.

Inkscape Inkscape.org Inkscape Forum Beginners' Questions Batch conversion of PDF to EPS via command line