Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Write in a spreadsheet, then "export" to twine

Hi everybody.
I'm working on a quite complicated non-linear story, and for convenience reasons, I'l using a spreadsheet to write it. My first column is each passage's pitch, my second column is a unique ID for each passage, and my third column is the ID of the next passage the current passage should link to (I hope that's clear, if not don't hesitate to ask for details).
To playtest my story, I'd love to make a Twine out of it, but the sheer idea of copying/pasting all the passages to Twine's classic building interface makes me go nuts (I work on a netbook with a very small screen, plus ther are A LOT of passages).
So I was wondering if there was some existing code to ask twine to go fetch passages in the spreadsheet and build the story on its own. I guess that sounds a bit lasy, but that actually would save me a lot of time and be a huge help.
Just to make things clear, my Twine level is quite low. I wrote a dozen of Twines, but I almost always only used the base features. I don't even know where I'm supposed to write additional code, should I have to!
Hope someone will be able to help... Thanks for reading anyway!

Comments

  • Your best best is exporting from the spreadsheet to a text file of Twee source code, and then importing it into a Twine file.

    Make an example Twine game in the interface then select File, Export, Twee Source code. If you can get your export from the spreadsheet to look like that file,
    you can then go File, Import, Twee Source Code and bring everything back into Twine.
  • It is not clear to me exactly what your spreadsheet looks like. If it's a non-linear story, some passages would have to have more than one outgoing link, how would that be represented in the spreadsheet?

    As nomdepony said, going via Twee source is probably the easiest way, since Twee is a much simpler file format than Twine (.tws). I don't know what spreadsheet you're using, but it probably has a CSV export feature. For example in LibreOffice Calc, you can do "Save As", set file type to "Text CSV", check "Edit filter settings" and select {Tab} as field delimiter. Then you could write a small script to convert the CSV to Twee source.

    Could you post a small example CSV export here, so we can see what the format looks like?
  • Thanks a lot for your answers.
    @mth : you are right : there are, in fact, three columns, because there may be, at some point, up to 3 choices. But most of the main structure is linear.
    Here's a .csv export. I've unfortunately been unable to export it with tabs as field delimiters, so there are commas instead. Sorry about that!
    You already helped me a lot there. I guess that, playing around in excel, I might export something close to a Twee. But should you tell me how I could switch from this .csv to a twee, I would be super grateful!

  • I thought "I'll write a script for that, it will only take a few minutes", but it turned out to be a bit more complex. Still, I managed to get something that should be enough to get you started. See attachment.

    One thing that wasn't clear to me is what to use for the text in the links to the following passages. The only thing I had to work with was the body of the passage linked to, so I picked that, but that may not be the right solution always.
  • Well this is great and I really appreciate you taking the time to work someting out for me. Sadly, you overestimated my capacity to handle a python file : I don't even know what to do with it. :-/
    But I don't feel comfortable stealing any more of your time, so I'll try to set something up by exporting my spreadsheet to .csv format with extra colums I'll use as tags. With a couple of find/replace parsings, I should be able to end up with something that would look like a Twee file.
    Thanks again anyway, much appreiated!
  • The Python script can be run on the command line using "python csv2twee.py test_export.csv". This assumes you have Python version 2.x installed on your PC.

    It probably looks a bit intimidating if you don't have programming experience, but being able to automate conversions like this can be a huge time saver, so it might be worthwhile to learn a bit about it.
  • You are right, of course, I'm going to try and make it work.
    I've installed Python 2.7.6 without problem, but now I have a question: when I'm running Python through the command line, am I not supposed to tell it where the script to use and the CSV to convert are located on my hard drive ? As your script does not contain "test_export.csv" in it, I have the feeling I am missing something. Should I put them in a specific folder? And how should I "tell" the script where to put the converted twee file? (my guess is it will be by default put in the source directory)
    I read your code : thanks for commenting it ! I think I get the logic. I'm not familiar with the syntax at all, but
    Plus, did you use the .csv file I sent, or did you reformat it in any way before converting it? I ask because my spreadsheet became ever more complicatd since I sent it to you, so I need a pattern to export my new .csv and be sure it will be understood by your script.
    Thanks a lot !
  • [quote author=poulet_fr link=topic=755.msg1752#msg1752 date=1390982887]
    I've installed Python 2.7.6 without problem, but now I have a question: when I'm running Python through the command line, am I not supposed to tell it where the script to use and the CSV to convert are located on my hard drive ? As your script does not contain "test_export.csv" in it, I have the feeling I am missing something. Should I put them in a specific folder? And how should I "tell" the script where to put the converted twee file? (my guess is it will be by default put in the source directory)
    The name of the input file must be given on the command line; "test_export.csv" is an example name. If the input file is in a different directory, you can specify a path to it, for example "python csv2twee.py ..\docs\story.csv" or "python csv2twee.py c:\temp\game.csv". The output file (Twee) will be written in the same directory as the input file (CSV).

    [quote author=poulet_fr link=topic=755.msg1752#msg1752 date=1390982887]
    Plus, did you use the .csv file I sent, or did you reformat it in any way before converting it? I ask because my spreadsheet became ever more complicatd since I sent it to you, so I need a pattern to export my new .csv and be sure it will be understood by your script.

    I used the .csv file you posted as-is.

    The "parseFile" function, in particular the big if-elif-else block (line 42 to 60 of the file) is where a row of the spreadsheet is processed. It looks at the number of columns in that row (len(values)) and depending on that it might skip the row (for 0 or 1 columns) or use the different columns to populate the Twee file (for 2 or more columns).
  • Great !
    Should I also specify the adress of the .py file in the command line ? I made a quick try and a program window briefly popped up then disappeared, with no result though. I'll investigate it further  :)
  • [quote author=poulet_fr link=topic=755.msg1755#msg1755 date=1390985935]
    Should I also specify the adress of the .py file in the command line ?

    If it is not in the working directory, yes. The working directory can be changed with the "cd" command.

    [quote author=poulet_fr link=topic=755.msg1755#msg1755 date=1390985935]
    I made a quick try and a program window briefly popped up then disappeared, with no result though.

    Are you using the DOS-like command line? ("cmd.exe")
  • Got it.
    I got a "python is not an internal command, external command, program, or command file." issue, but I found a way to fix this by typing "PATH=C:\Apps\Python;%PATH%" in the command box (found this on the net). I still don't know how to definitely add python to PATH in windows (XP), but that will do the trick for now.
    I did a test run and it worked great !
    Now I'll have to play around in your code a bit to adapt it to my new spreadsheet. For instance I now have a column with variable declaration and modification in it, which I wrote in twine-style (i.e <<set $variable_name = "value">>. I think I should be able to sort this out.
    Thanks for everything !
  • Hi there.
    So I *almost* got it.
    As I said, I rewrote your script for it to take into account 2 new columns I created. Those are in position 2 and 3, one concerns variables modification, and the other is aimed at printiong out the state of every variable in every text passage (for debugging purposes).
    So now the structure is :
    column 1 : text (to be printed out)
    column 2 : action on variables (to be printed out as I wrote it in twine code)
    column 3 : feedback (to be printed out as I wrote it in twine code)
    columns 4 to 7 (there are up to 4 possibilities now) : links to following passages (to be associated with according passage's texts)
    To deal with my 2 extra columns, I added things in your script on rows 55, 56, 65, 84, 90, 94 and 95.
    Well, I must have done something wrong, because when I tried to convert the attached spreadsheet, the DOSbox gave me an error message :

    [quote]Traceback (most recent call last) :
    File "csv2twee.py", line 110, in <module>
    convert<fileName>
    File "csv2twee.py", line 77, in convert
    data = parseFile<inp>
    File "csv2twee.py", line 57, in parseFile
    passageID = values [3]
    IndexError: list index out of range

    And sadly, no file was created.
    It is even more frustrating as I feel I'm close to target here. So if you care to (once again) help... well that would be great, as usual !
  • The IndexError means that an attempt is made to fetch a list element that doesn't exist. In particular, the list "values" doesn't have an element at index 3, which means the list has 2 or 3 elements (0 and 1 are handled elsewhere in the script).

    Since index 3 contains the passage ID, I'm guessing every row should have at least that many elements. If so, modify this line:

    elif len(values) == 1:
    # Second column is empty.
    print >>sys.stderr, 'Skipping text without ID: "%s"' % values[0]
    to:

    elif len(values) <= 3:
    # ID column is empty.
    print >>sys.stderr, 'Skipping text without ID: "%s"' % values[0]
    It still won't process the offending row then, but it will at least print a proper warning instead of crashing.

    If it is valid for some rows to have 1, 2 or 3 columns, you should add code to check for that ("len(values)" is the number of columns) and handle it appropriately.
Sign In or Register to comment.