Friday, August 17, 2007

Text to HTML Table Converter 1.0

Background


I was recently creating an description for a videogame I was selling on eBay and I needed to convert some text into an HTML table. Since I also was learning Python at the same time, I figured I would make a Python program that would do the job.

Input text format


The input text needs to be at least one line of text or more(seperated by a newline '\n') with a colon seperating the <th> element on the left and the <td> element on the right. You could also have another delimiter separating the two elements by changing the tList = eachLine.split(':') line to split based on whatever character you want.


The general format of the input text file should be:


thElement1: tdElement1
thElement2: tdElement2
...


where the <th> and <td> elements can have spaces, tabs, and other characters in them as long as they aren't '\n' or ':'.

Output HTML Format


The output which is displayed in the console (stdout) and which can optionally be saved to a file has this general format:


<table>
<tr>
<th>thElement1: </th>
<td>tdElement1</td>
</tr>

<tr>
<th>thElement2: </th>
<td>tdElement2</td>
</tr>
</table>


Download


This program is Copyright 2007 to Boris Joffe and licensed under GPL 3.0. Open the file to read more of the terms that I released it under.
Download Text to HTML Table (txt2html-table) Version 1.0

Usage Instructions


First of all, you need to download Python on your computer. If you're using a *NIX system, it should already by installed. The basic syntax is

python txt2html-table-1.0.py [-f] [-v] input.txt

If you don't include any of the optional flags (-f or -v), it will output the text in the file and confirm if you want to convert it. Type "y" or "yes" without the quotes to confirm or any other character(s) to exit. Upon confirmation, it will convert it to an HTML table and output the results.


Example: With no extra options


$ python txt2html-table-1.0.py input.txt
Would you like to convert the text below into an HTML Table?
=======================
thElement1: tdElement1
thElement2: tdElement2
=======================
< Yes | No >: y
Converted successfully to:
=======================
<table>
<tr>
<th>thElement1: </th>
<td>tdElement1</td>
</tr>

<tr>
<th>thElement2: </th>
<td>tdElement2</td>
</tr>
</table>
=======================


Other command line options/flags


Use:

$ python txt2html-table-1.0.py -f input.txt

to prompt for a filename at the end of a conversion to save the html output to. If you decide to not save the output, press ENTER when it prompts for a filename in order to quit.

Example: Use of -f flag


$ python txt2html-table-1.0.py -f input.txt
Would you like to convert the text below into an HTML Table?
=======================
thElement1: tdElement1
thElement2: tdElement2
=======================
< Yes | No >: y
Converted successfully to:
=======================
<table>
<tr>
<th>thElement1: </th>
<td>tdElement1</td>
</tr>

<tr>
<th>thElement2: </th>
<td>tdElement2</td>
</tr>
</table>
=======================
Enter a filename (Hit ENTER to quit): output.html
Output saved to: output.html


Use:

$ python txt2html-table-1.0.py -v input.txt

for verbose output. It shows you what elements each line was split into.

Example: Use of -v flag


$ python txt2html-table-1.0.py -v input.txt
Would you like to convert the text below into an HTML Table?
=======================
thElement1: tdElement1
thElement2: tdElement2
=======================
< Yes | No >: y
' thElement1: tdElement1 ' was split into ['thElement1', 'tdElement1']
' thElement2: tdElement2 ' was split into ['thElement2', 'tdElement2']
Converted successfully to:
=======================
<table>
<tr>
<th>thElement1: </th>
<td>tdElement1</td>
</tr>

<tr>
<th>thElement2: </th>
<td>tdElement2</td>
</tr>
</table>
=======================

You can combine these two flags in either order as long as they're between the python file (.py) and the input filename. However, you cannot type in "-fv" or "-vf".

Errors you might receive



  • "Please pass a filename to me" if you didn't supply any arguments to the program.

  • "File does not exist." if the last argument you gave was not a valid file. Could occur if you added the flags after the input filename.


To avoid other errors, make sure that the python interpreter is in your OS's path, the program is in the current directory, and each line in your input file has one colon separating the <th> and <td> elements.

Todo: Possible features in future versions



  • Allow for specifying the output filename as a parameter i.e. python txt2html-table-1.0.py -f output.html input.txt

  • Allow more than two elements per line i.e. all elements after the first would be extra <td> elements in the table
  • Allow unbuffered reading in of text from the command line(stdin)

  • Add better support for flags (allow -fv or -vf)


Alternatives


Use the Python DOM library to build the table instead of raw HTML-holding variables like tHead, tBody, and tTail


Tools


Used Quick Escape to escape the html output for inserting into this post.

No comments: