Convert CSV to XLS

1,331 views
Skip to first unread message

Eric Bunton

unread,
Jul 7, 2011, 3:59:18 PM7/7/11
to python-excel
Can anyone give me an script that will convert a csv file to xls or
tell me what is needed?

John Machin

unread,
Jul 7, 2011, 5:21:03 PM7/7/11
to python...@googlegroups.com
On Friday, 8 July 2011 05:59:18 UTC+10, Eric Bunton wrote:
Can anyone give me an script that will convert a csv file to xls or
tell me what is needed?

Read your input file row by row using the python csv module.
Write your output file using the xlwt module -- see the tutorial that you can access via www.python-excel.org.
Note that the csv reader will deliver byte strings for each field (unless you are using the QUOTE_NONNUMERIC option). If you want better than that, you will need to know what data type should be in each column (or write code to infer it). You will also need to know what encoding has been used to write the CSV file.

Cheers,
John

John Ring

unread,
Jul 7, 2011, 5:23:55 PM7/7/11
to python...@googlegroups.com
You could always do it the good old fashion way. Split on the commas and '\n' and write to a .xls using the excel functionality from the win32.com module.

--
You received this message because you are subscribed to the Google Groups "python-excel" group.
To view this discussion on the web, visit https://groups.google.com/d/msg/python-excel/-/zYThSOA98Y8J.

To post to this group, send an email to python...@googlegroups.com.
To unsubscribe from this group, send email to python-excel...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/python-excel?hl=en-GB.

John Machin

unread,
Jul 7, 2011, 5:32:09 PM7/7/11
to python...@googlegroups.com
On Friday, 8 July 2011 07:23:55 UTC+10, John_Ring wrote:
You could always do it the good old fashion way. Split on the commas and '\n' and write to a .xls using the excel functionality from the win32.com module.

"split on the commas" will die in a hole if there are any commas or quotes in the data. Why do you think anybody bother to write the python csv module if it were that simple??

If you are going to use pywin32 for output, use it for input as well.

John Yeung

unread,
Jul 7, 2011, 5:52:50 PM7/7/11
to python...@googlegroups.com
On Thu, Jul 7, 2011 at 5:23 PM, John Ring <jrin...@gmail.com> wrote:
> You could always do it the good old fashion way. Split on the
> commas and '\n' and write to a .xls using the excel functionality
> from the win32.com module.

That may be the old-fashioned way (prior to Python 2.3) but it is far
from being a good way. If you care at all about reading the data
correctly, you really should just use the csv module.

As for COM, it's slower and requires the script to be running on a PC
with Excel installed. (Not to mention pywin32 installed.) Unless you
really need something from Excel that cannot be written with xlwt,
just use xlwt.

And, as John Machin suggests, if you are going to use COM, you might
as well use it for input as well. (Though personally, at that point I
tend to just go with VBScript instead of Python.)

John Y.

Reply all
Reply to author
Forward
0 new messages