automate text input from .txt and place in inkscape?

Discussion about writing code for Inkscape.
paulsomlo
Posts: 7
Joined: Sat Sep 30, 2017 4:04 am

automate text input from .txt and place in inkscape?

Postby paulsomlo » Sat Sep 30, 2017 4:17 am

I'm making buttons (name badges) for an event. In Inkscape, each button will consist of graphics inside of a circle. Each page will contain 12 buttons in three columns of four. I also have the names in a .txt file, one name per line. Is it possible to automate the process? I would like an extension/script that can fetch the names from the .txt file, change the font and size, then place them in the individual circles, having the "center" of the name coincide with the center of it's corresponding circle.

The alternative, is that I have to type 140 or so names into Inkscape and manually place them, which is painful.

Paul

Moini
Posts: 2045
Joined: Mon Oct 05, 2015 10:44 am

Re: automate text input from .txt and place in inkscape?

Postby Moini » Sat Sep 30, 2017 7:30 am

If you use a CSV file instead, you can use the Generator extension:
http://wiki.inkscape.org/wiki/index.php ... #Generator
For usage instructions, see linked example files.
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!

Inkscape FAQ - Learning Resources - Website with tutorials (German and English)

Moini
Posts: 2045
Joined: Mon Oct 05, 2015 10:44 am

Re: automate text input from .txt and place in inkscape?

Postby Moini » Sat Sep 30, 2017 7:30 am

Oh, sorry, overlooked that it needs to change the font size - that's currently not possible.
But both centering and adapting the size should be possible via script...
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!

Inkscape FAQ - Learning Resources - Website with tutorials (German and English)

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

Re: automate text input from .txt and place in inkscape?

Postby tylerdurden » Sat Sep 30, 2017 8:34 am

If there are only 140 names, copy /paste into a template is not so bad, and would probably be faster than making a script.
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/

Moini
Posts: 2045
Joined: Mon Oct 05, 2015 10:44 am

Re: automate text input from .txt and place in inkscape?

Postby Moini » Sat Sep 30, 2017 12:04 pm

It's not a totally unusual request, I've seen this asked a couple of times. It's probably not even too difficult to do...
I'd make the script create the text in the standard font size first, then I'd ask Inkscape how wide the text is, and then, if too wide, I'd scale it down. Then center on object with specified ID.
Another approach could make the texts all the same width, but that runs the risk of getting a too tall text, when it's short, so that's not really better.
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!

Inkscape FAQ - Learning Resources - Website with tutorials (German and English)

User avatar
brynn
Posts: 9534
Joined: Wed Sep 26, 2007 4:34 pm
Location: western USA
Contact:

Re: automate text input from .txt and place in inkscape?

Postby brynn » Sun Oct 01, 2017 10:09 pm

It would make for a nice extension, if an Inkscape extension could do that.

paulsomlo
Posts: 7
Joined: Sat Sep 30, 2017 4:04 am

Re: automate text input from .txt and place in inkscape?

Postby paulsomlo » Mon Oct 02, 2017 4:18 am

Well, it sort of worked. While it inherited the font and size from %VAR_name%, which is very nice, the positioning was not consistent. I was able to get the positioning correct for one name, but not for other names. Vertically, all the names were in the correct place, but horizontally the names shifted, seeming to correspond to the size of the name, but not the number of letters. Also, the output consists of one file for each name. Ideally, I would like to start with 12 graphics on a page and be able to place a different name in each graphic.

paulsomlo
Posts: 7
Joined: Sat Sep 30, 2017 4:04 am

Re: automate text input from .txt and place in inkscape?

Postby paulsomlo » Mon Oct 02, 2017 4:22 am

I compared some output files. It appears that the first letter in each name begins in the same place, consistently.
How does the .svg file know how to position the text?

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

Re: automate text input from .txt and place in inkscape?

Postby tylerdurden » Mon Oct 02, 2017 6:58 am

paulsomlo wrote:I compared some output files. It appears that the first letter in each name begins in the same place, consistently.
How does the .svg file know how to position the text?

Usually by the justification buttons in the text controls... for left/center/right/jusified. Positions a "text anchor" that is the snap-point for the text object.
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/

paulsomlo
Posts: 7
Joined: Sat Sep 30, 2017 4:04 am

Re: automate text input from .txt and place in inkscape?

Postby paulsomlo » Tue Oct 03, 2017 6:53 am

tylerdurden wrote:
paulsomlo wrote:I compared some output files. It appears that the first letter in each name begins in the same place, consistently.
How does the .svg file know how to position the text?

Usually by the justification buttons in the text controls... for left/center/right/jusified. Positions a "text anchor" that is the snap-point for the text object.

Doesn't work as expected.

paulsomlo
Posts: 7
Joined: Sat Sep 30, 2017 4:04 am

Re: automate text input from .txt and place in inkscape?

Postby paulsomlo » Tue Oct 03, 2017 10:52 am

Moini wrote:It's not a totally unusual request, I've seen this asked a couple of times. It's probably not even too difficult to do...
I'd make the script create the text in the standard font size first, then I'd ask Inkscape how wide the text is, and then, if too wide, I'd scale it down. Then center on object with specified ID.
Another approach could make the texts all the same width, but that runs the risk of getting a too tall text, when it's short, so that's not really better.

I've looked at the XML file and the coordinates given for text remain a mystery to me. They apparently don't correspond to the center and I don't know if they correspond to an edge, a corner, one of the "handles", or what. I finally found a way to center the text - I drew two vertical lines, placed each one at the periphery of the circle, diametrically opposed. I then centered %VAR_name% between them using the "distribution" function, rather than "align". Then using "stroke", I made the two vertical lines invisible. Now, my text is centered in the circle, but I'd like to be able to fill the circle equally for different size names. If I set the font size of %VAR_name% large, "Bob" works, but not so good for "Caroline". So, getting back to your suggestion Moini, how do I extract the width of the text? I see it displayed in inkscape, but not in the corresponding XML file. Or maybe I don't need to extract the text width, I just need to know how wide a space I have to fill. Then, I need to figure out how to scale the text to fit.

Moini
Posts: 2045
Joined: Mon Oct 05, 2015 10:44 am

Re: automate text input from .txt and place in inkscape?

Postby Moini » Wed Oct 04, 2017 1:14 am

The centering can be done via setting text justification in the template file, as TylerDurden wrote (and of course, one must take care that the text center for the %blah% thing is in the correct position, as you found out).

The automatical adjustment of the width of the text or the font size would need to be done by editing the extensions program code. Inkscape can return an object's width via the command line, and it can also be set there.

If you want to work on the extension, see https://inkscape.org/en/develop/extensions/ and https://inkscape.org/doc/inkscape-man.html.
You can remove the helper lines, they don't need to be made invisible - unless you want to reuse them for all 144 names, and do the centering manually.
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!

Inkscape FAQ - Learning Resources - Website with tutorials (German and English)

paulsomlo
Posts: 7
Joined: Sat Sep 30, 2017 4:04 am

Re: automate text input from .txt and place in inkscape?

Postby paulsomlo » Wed Oct 04, 2017 11:25 am

I got the centering to work, for the most part - longer names need a slight horizontal adjustment. I was able to figure out how to query the text width from the command line, but am not able to figure out how to change it from the command line. I can't find an inkscape verb that seems to do this directly. I also noticed that the XML file does not give font width, it gives font size in pixels, which corresponds to the height of the font as given in the svg file.

Moini
Posts: 2045
Joined: Mon Oct 05, 2015 10:44 am

Re: automate text input from .txt and place in inkscape?

Postby Moini » Thu Oct 05, 2017 2:04 am

Yes, sorry for causing confusion. There's no direct verb for this. You'd need to use the python-based XML-editing extensions part. Take a look at other extensions that do scaling. They use functions from simpletransform.py. It will take a combination of both the bash and the python approach, due to the inconvenience of verbs not taking any parameters.
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!

Inkscape FAQ - Learning Resources - Website with tutorials (German and English)

Moini
Posts: 2045
Joined: Mon Oct 05, 2015 10:44 am

Re: automate text input from .txt and place in inkscape?

Postby Moini » Thu Oct 05, 2017 2:15 am

You'd have to set a scale transform for the text, I think.
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!

Inkscape FAQ - Learning Resources - Website with tutorials (German and English)

paulsomlo
Posts: 7
Joined: Sat Sep 30, 2017 4:04 am

Re: automate text input from .txt and place in inkscape?

Postby paulsomlo » Fri Oct 06, 2017 4:18 pm

Well, I finished up this evening. I never did figure out how to scale the text; I chose a font size that would make the shortest names large enough to read, then went through the files with long names and manually reduced their size. I put 12 graphics on each page and did so manually. I may find a way to automate that in the future, but it only took about an hour and a half to place 200 something graphics into 20 pages of 12 each.

I did learn quite a bit about Inkscape, and some of my shell programming is coming back to me. Thank you all for your help.

Moini
Posts: 2045
Joined: Mon Oct 05, 2015 10:44 am

Re: automate text input from .txt and place in inkscape?

Postby Moini » Sat Oct 07, 2017 6:37 am

Will you share the modified code?
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!

Inkscape FAQ - Learning Resources - Website with tutorials (German and English)


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 1 guest