Any way to do bulk changes to SVGs?

Post questions on how to use or achieve an effect in Inkscape.
shorvok
Posts: 2
Joined: Fri Jul 24, 2015 6:30 am

Any way to do bulk changes to SVGs?

Postby shorvok » Fri Jul 24, 2015 6:41 am

Hi, the work I'm doing changed their quality standards and now I have several thousand SVGs that need to be fixed.

Essentially I need to change every SVG so that the page color is changed from whatever it was set as to #000000

For example, in the code:

Code: Select all

pagecolor="#666666"
becomes

Code: Select all

pagecolor="#000000"


I also need to remove all excess clip paths from the SVGs, or effectively run the Vacuum Defs command under File.

Does anyone know if it is possible to do this with a script or some way that I can do it quickly, because it is a very large task to do manually.

Thank you.

tylerdurden
Posts: 2344
Joined: Sun Apr 14, 2013 12:04 pm
Location: Michigan, USA

Re: Any way to do bulk changes to SVGs?

Postby tylerdurden » Fri Jul 24, 2015 11:47 am

Not sure about command-line for cleanup, but bulk page color change is a piece of cake...

Open all files in Textpad (or similar) and perform global replace.
Have a nice day.

I'm using Inkscape 0.92.2 (5c3e80d, 2017-08-06), 64 bit win8.1

The Inkscape manual has lots of helpful info! http://tavmjong.free.fr/INKSCAPE/MANUAL/html/

~suv
Posts: 2272
Joined: Sun May 10, 2009 2:07 am

Re: Any way to do bulk changes to SVGs?

Postby ~suv » Fri Jul 24, 2015 6:27 pm

I'd use a shell one-liner for each task (the two could also be merged to only loop through all files once, and stored as shell script file).

To change the background color:

Code: Select all

for f in *.svg; do sed -e 's/pagecolor="#666666"/pagecolor="#000000"/' -i "" "$f" ; done
(for personal usage, I probably would be more careful and save the modified files to a separate folder, or with a different file name).

To clean-up the documents, something like this (untested):

Code: Select all

for f in *.svg; do inkscape --file="$f" --verb="FileVacuum" --verb="FileSave" --verb="FileQuit"; done
(for personal usage, I'd probably run this with an X11-based Inkscape build, using a X virtual framebuffer like Xvfb to hide the Inkscape GUI if there were thousands of files to be cleaned up using Inkscape via script or command line (the GUI cannot be disabled when using verbs on the command line; verbs cannot be used in inkscape shell mode -> each file needs to be cleaned up in a separate Inkscape process which opens the file in the GUI, cleans up the file, saves it and quits inkscape again)


Hint: I would recommend to provide information at least about (preferred) OS/platform, and maybe about available tools and/or skills, too - otherwise some of the provided answers might not applicable in your specific context at all.

shorvok
Posts: 2
Joined: Fri Jul 24, 2015 6:30 am

Re: Any way to do bulk changes to SVGs?

Postby shorvok » Sat Jul 25, 2015 1:10 am

tylerdurden wrote:Not sure about command-line for cleanup, but bulk page color change is a piece of cake...

Open all files in Textpad (or similar) and perform global replace.


Ok, wow, that worked great. I hadn't even thought of that.

~suv wrote:I'd use a shell one-liner for each task (the two could also be merged to only loop through all files once, and stored as shell script file).

To change the background color:

Code: Select all

for f in *.svg; do sed -e 's/pagecolor="#666666"/pagecolor="#000000"/' -i "" "$f" ; done
(for personal usage, I probably would be more careful and save the modified files to a separate folder, or with a different file name).

To clean-up the documents, something like this (untested):

Code: Select all

for f in *.svg; do inkscape --file="$f" --verb="FileVacuum" --verb="FileSave" --verb="FileQuit"; done
(for personal usage, I'd probably run this with an X11-based Inkscape build, using a X virtual framebuffer like Xvfb to hide the Inkscape GUI if there were thousands of files to be cleaned up using Inkscape via script or command line (the GUI cannot be disabled when using verbs on the command line; verbs cannot be used in inkscape shell mode -> each file needs to be cleaned up in a separate Inkscape process which opens the file in the GUI, cleans up the file, saves it and quits inkscape again)


Hint: I would recommend to provide information at least about (preferred) OS/platform, and maybe about available tools and/or skills, too - otherwise some of the provided answers might not applicable in your specific context at all.



My apologies for not giving more info. I'm not proficient at coding so it's kind of difficult for me to figure out. Tylerdurden's recommendation worked great for the pagecolor, and I tried your script for cleanup, but it throws an error "Missing # on loop variable at line 1" and I'm not skilled enough at this to fix it.

~suv
Posts: 2272
Joined: Sun May 10, 2009 2:07 am

Re: Any way to do bulk changes to SVGs?

Postby ~suv » Sat Jul 25, 2015 1:55 am

shorvok wrote:My apologies for not giving more info. I'm not proficient at coding so it's kind of difficult for me to figure out. Tylerdurden's recommendation worked great for the pagecolor, and I tried your script for cleanup, but it throws an error "Missing # on loop variable at line 1" and I'm not skilled enough at this to fix it.

The one-liners I posted earlier were written for the standard Unix shell (Bourne shell, or Bash); it works on OS X and Linux-based desktops, but unlikely as is on Windows [1].

--
[1] At this point I assume that you use Windows - since the other tip recommended 'TextPad' and that one seems to have worked just ok for you. Good luck with the second part (I don't use Windows myself, and can't really help with batch scripting for that OS).


Return to “Help with using Inkscape”