How do I read and write to an excel file?

36 views
Skip to first unread message

Tollens

unread,
Oct 1, 2009, 4:30:19 PM10/1/09
to python-excel
I have been working with the packages xlwt and xlrd.

I have an excel file that has "xyz" in the first cell. All I want to
do is print the value of cell 0,0 and change the cell value to "abc"
then save the workbook.

It seems like all I can do now is either read the value with xlrd, or
write a new worksheet with xlwt.

Can anyone help me?

Chris Withers

unread,
Oct 1, 2009, 5:08:21 PM10/1/09
to python...@googlegroups.com
Tollens wrote:
> It seems like all I can do now is either read the value with xlrd, or
> write a new worksheet with xlwt.

Read the tutorial on http://www.python-excel.org.

Specifically, look for xlutils.copy

cheers,

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk

Tollens

unread,
Oct 1, 2009, 5:11:38 PM10/1/09
to python-excel
Hello Chris,

I read that a few times. I still can't get it figured out.

Here is what I have so far to read and write. I don't want to copy the
file, I just want to modify an existing cell.

from xlwt import Workbook
from xlrd import open_workbook

#Read
rb = open_workbook('_symbolx.xls')
rs = rb.sheet_by_index(0);
print rs.cell(0,0).value;

#Write
w = Workbook()
ws = w.add_sheet("Symbols")
ws.write(0,0,"abc");
w.save('_wes.xls')

Chris Withers

unread,
Oct 1, 2009, 5:32:04 PM10/1/09
to python...@googlegroups.com
Tollens wrote:
> Hello Chris,
>
> I read that a few times. I still can't get it figured out.
>
> Here is what I have so far to read and write. I don't want to copy the
> file, I just want to modify an existing cell.

You want to copy the file.

Use the xlutils.copy example:

from xlrd import open_workbook
from xlutils.copy import copy
rb = open_workbook(...,formatting_info=True)


rs = rb.sheet_by_index(0);
print rs.cell(0,0).value;

wb = copy(rb)
wb.get_sheet(0).write(0,0,'abc')
wb.save(...)

Tollens

unread,
Oct 1, 2009, 6:00:25 PM10/1/09
to python-excel
Chris, you are brilliant!

Thank you for the help, it works perfectly

Reply all
Reply to author
Forward
0 new messages