txt2xls

M

Michele Simionato

In would like to convert very simple text files into .xls
files and I need some help.

Here is the format of a typical file:

# begin table.txt

This should go in the first row,
first column.

This should go in the first row,
second column.

====

This should go in the second row,
first column.

This should go in the second row,
second column.

====

This should go in the third row,
first column.

This should go in the third row,
second column.

# end table.txt

"====" separates different rows, whereas different colums are separated
by an empty line. table.txt should be converted into a table.xls
file containing a 3x2 table like this:

================================ ================================
This should go in the first row, This should go in the first row,
first column. second column.
-------------------------------- --------------------------------
This should go in the second row, This should go in the second row,
first column. second column.
-------------------------------- --------------------------------
This should go in the third row, This should go in the third row,
first column. second column.
================================ ================================

I use OpenOffice on Linux, but I have no experience at all about the .xls
format, so I would welcome help/pointers and especially snippets of code ;)
TIA,

Michele Simionato
 
M

Michele Simionato

Benjamin Niemann said:

Thank you! The distribution contains an example that does exactly what I
want (with minor changes). I report here a snippet just for future
reference:



import pyXLWriter as xl

workbook = xl.Writer(xlsfilename)
worksheet = workbook.add_worksheet()


nrow = 0
for chunk in file(textfile).read().split("\n====\n"):
row = chunk.split('\n\n') # Split on empty line
for ncol, cell in enumerate(row):
worksheet.write([nrow, ncol], cell.strip())
nrow += 1

workbook.close() # needed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,205
Messages
2,571,067
Members
47,673
Latest member
MahaliaPal

Latest Threads

Top