Turning a Text List into Text Objects

I quite enjoy board games and story telling games, and have had a crack at a few of them including the Nettlebed Caverns game which is available in a draft form on this webpage (www.techmonkeybusiness.com/nettlebed-caverns.html). One of the hall marks of my story telling games is that they have a HUGE number of cards....like 300 or so. This gives a lot more scope for variety in the stories and, well actually, it is a heap of fun making up what is going onto the cards.

Writing out the list of cards in a word processor is fun. Writing them out again into a graphics package of some sort to lay them out as cards is a bit of a chore, particularly when you are dealing with 300 or more of them. So I felt there was a need for a simple script to take a list of text, and turn it into a heap of text elements in a vector based graphics program. Once the text was in the graphics program I could then use its tools to lay out the text nicely.

I knew that Inkscape's (https://inkscape.org/) native (SVG) was just an XML so this was an obvious choice for the output. After a couple of tests, I observed that there was a block of unchanging XML at the start, and a convenient XML block for each text element. This meant that creating a quick and dirty python script that took each line in a text file and turned it into a separate text element in an SVG was relatively simple. I did not bother to use the script to position the text elements because I could use the Inkscape Object Distribution tools to spread it out once I had them in Inkscape.

The python script does not use any libraries and so will run on the most basic python installation. As you will see, the vast majority of the script is putting the XML header into the file created.

You can download the python script here: http://www.techmonkeybusiness.com/Code/ TXT2InkscapeXMLv1.py

# TXT2InkscapeXMLv1.py

# A simple script to look through a list of text and # turn each line into an xml entry for use in Inkscape. # in this script we will just dump all of the text # ontop of itself, irrespective of the actual layout # then use inkscape to spread it out. ofilenm = str(input("What is the filename you wish for output?")) outfile = open(ofilenm,"w")

ListFlm = str(input("What is the name and location of the TXT file to work with?"))

# The block that follows is the Inkscape standard header. outfile.write('\n') outfile.write('\n') outfile.write('\n') outfile.write('\n') outfile.write(' \n') outfile.write(' \n') outfile.write(' \n') outfile.write(' \n') outfile.write(' \n') outfile.write(' image/svg+xml\n') outfile.write(' \n') outfile.write(' \n') outfile.write(' \n') outfile.write(' \n') outfile.write(' \n') outfile.write(' \n')

# The section that follows is to build the SVG text elements f = open(ListFlm,"r") for line in f: TXTLine = line.strip() outfile.write(' '+TXTLine+'\n') #This is the text element from the list

# The next section is the close off for the SVG xml. outfile.write(' \n') outfile.write('\n') outfile.close()

How to use the script To use the script follow the steps outlined below: • Save your list as a plain TXT file that only contains the list of items you wish to appear in the Inkscape illustration. • Start up your python environment. I use Pyzo (www.pyzo.org/), but you could equally well use the terminal to run the script if you have an with python already present. • Load and run the script. • The script will ask you what you want to call the file it produces. Give some name with the .svg suffix. Your file will appear in the User directory for Systems and possibly in the “My Documents” directory in Windows based systems. • The script will then ask you to identify the list file that you are wanting processed. You will need to include the pathname, eg /home/user/Documents/junk/List.txt. • An instant later it will have the command prompt back up and you will wonder if anything happened. You should see your new .svg file in the User directory. • Open the .svg in Inkscape.

• You will be faced with a black smear of text in the Inkscape document.

• Click on the pile of text to select the topmost one, and drag it somewhere down the page.

• Drag a box around all of the text elements to select them all. • Activate the Object >Align and Distribute panel (Shift+Ctrl+A)

• “Distribute centres equidistantly vertically” button to spread the text elements evenly between the top text element and the text element you dragged down the page. • In my case where I wish to create a grid of cards, there is a panel for distributing elements across a grid. You can find this under menu Object > Rows and Columns. So now the list is a distributed series of text objects in Inkscape without having to retype them in. Yayyy!

www.techmonkeybusiness.com