Skip to Content
 

Idea: Batch card making with XML substitution

4 replies [Last post]
larienna
larienna's picture
Offline
Joined: 07/28/2008

This is just an idea that came to my mind while responding to another thread. It could be use in the case where:

  • Need to make cards or tokens
  • Need 1 file per card or token
  • Want to use a CSV file with all the card information
  • Want to use a template for each of those cards

In therory, the process is just copy pasting content from the CSV into the card template and repeat the process for each card.

So I was thinking, what if I designed a template in Inkscape and I identified each field that could receive content with a variable name. Then, since .svg files are XML files, I could scan those files and substitutes the content of the text field with the corresponding content in the XML file. That would allow me to generate multiple cards in batch.

Here is the definition of a text field by Inkscape. "InfoField" is the variable name, and "Hello" would the the text to replace.

<text
    xml:space="preserve"
    style="font-style:normal;font-weight:normal;font-size:10.58333302px;line-height:125%;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#ffffff;fill-opacity:1;stroke:none;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
    x="17.008928"
    y="21.266367"
    id="text3717"
    inkscape:label="InfoField"><tspan
      sodipodi:role="line"
      id="tspan3715"
      x="17.008928"
      y="21.266367"
      style="fill:#ffffff;stroke-width:0.26458332px">Hello</tspan></text>

Nandeck can probably do something similar already but in a different way.

pelle
pelle's picture
Offline
Joined: 08/11/2008
That sounds like exactly what

That sounds like exactly what my Inkscape extension does? Have you managed to not see any of the posts I made about that here over the years? :(

https://boardgamegeek.com/thread/299033/inkscape-extensions-boardgame-de...

If you just want to run it as a script from the command-line instead of running it inside of Inkscape (or to run it from some build-system) there are some instructions for that:

https://github.com/lifelike/countersheetsextension/wiki/Running-from-Com...

(Somewhat outdated. Needs python 3 to run these days.)

Unfortunately SVG is a bit more complex than you might have seen. If you carefully construct your own XML you can avoid the mess that an Inkscape-created SVG can be. But then if you want to do things like inserting multi-line text it becomes a bit tricky because the support in SVG is not great so you have to do some work. And there are things like inserting an icon in text that is not really supported at all by Inkscape (even if an old SVG element for that exists) so then some tricks are needed. Inkscape itself does a lot of tricks too to support many things that are not actually supported by SVG (like layers).

Of course you can have a look at my source code. It is a bit messy after 14 years of development, but hopefully some parts are somewhat readable.

If it does not do exactly what you want it to do you can always modify it. If you do not mess things up too badly I am happy to accept patches. Others have sent code to add features they needed and I am sure there are more things that could be done that someone can think of.

larienna
larienna's picture
Offline
Joined: 07/28/2008
Yes, I remember your

Yes, I remember your extension a bit. I think you could generate sets of tokens and hex grid.

Else, simple text substitution of the content of a template with a tool like sed could probably work to substitute keywords with content. No need to know the XML.

For icons and images, that could indeed be more complicated especially for embedded images. Maybe linking images could be possible.

nand
nand's picture
Offline
Joined: 07/27/2008
larienna wrote:Nandeck can

larienna wrote:
Nandeck can probably do something similar already but in a different way.

Yes, I had created nanDECK thinking that rtf, html or svg were too complex to batch edit manually. An early script was something like this, just one line for the variable text, one for the font, and one for the text:

[text]=one|two|three
font=arial,32,,#000000
text=1-3,[text],0,0,6,9

pelle
pelle's picture
Offline
Joined: 08/11/2008
Replacing parts of text in

Replacing parts of text in SVG or things like filenames and most attributes would be trivial using just very simple tools like sed. That would absolutely work. It would be easy to write a script to just copy a SVG file and make some substitutions. You could set up a master document with the layout you want for sheets, with image-elements for each card, and then just replace the file-urls in the image-elements to point to the generated cards, and then you would basically have all you need to quickly make an entire card game. A bit more manual work to set up than if using something like my Inkscape extension, but realistically if you are not setting up a toolchain for a brand new game every month anyway I can't claim that you need a more advanced tool to save time.

Then there are parts of SVG that are far worse than that and will not work with simple string-substitution unfortunately so you'd have to switch to parse the XML (and then parse some of the attribute strings in the XML, that can be pretty complex on their own). That is far more work to get right. Figuring out how to transform all the SVG transforms correctly is really painful, and I already mentioned seemingly easy things like multi-line text that is a mess. And then every new version of Inkscape changes things around a bit to make things more exciting.

Syndicate content


forum | by Dr. Radut