By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,024 Members | 2,027 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,024 IT Pros & Developers. It's quick & easy.

Writing to and Reading from Excel

P: 17
Hi,

I did some searching online and I found that I could use

xlrd and win32com.client

It seems that I could use win32com.client to read and write to excel. However, I didn't see xlrd do that. Is there a way to do that?
Jun 20 '08 #1
Share this Question
Share on Google+
4 Replies


jlm699
100+
P: 314
Yes it can read. Refer to xlrd's API here.

First line reads:
"A Python module for extracting data from MS Excel spreadsheet files. "

So it doesn't look like you can write to data... but you could extract the data, do what you need with it, and then save it back as a CSV, which can then be opened by Excel
Jun 20 '08 #2

P: 17
Yes it can read. Refer to xlrd's API here.

First line reads:
"A Python module for extracting data from MS Excel spreadsheet files. "

So it doesn't look like you can write to data... but you could extract the data, do what you need with it, and then save it back as a CSV, which can then be opened by Excel
My bad I meant I could read it, but I don't know how to write to it. I have never worked with CSV. Is there an example?
Jun 20 '08 #3

P: 17
Is there something bad about using win32com.client? If not, then I will use this script. Seems to work fine.

import win32com.client
xlApp = win32com.client.Dispatch("Excel.Application")
xlApp.Visible=1
xlWb = xlApp.Workbooks.Open("Read.xls")
print xlApp.Worksheets("Sheet1").Name
xlApp.Worksheets("Sheet2").Range("A1").Value = "yellow"
cell = xlApp.Worksheets("Sheet3")
cell.Range("C3").Value = "money"
cell.Range("D4").Value = 9999
print cell.Range("C3").Value
print cell.Range("D4").Value
xlWb.Close(SaveChanges=1)
xlApp.Quit()
Jun 20 '08 #4

jlm699
100+
P: 314
There is nothing bad about using COM objects; aside from the fact that you cannot develop a cross-platform application. You'll be stuck to windows (but considering it's Excel you probably won't be trying to get it to work under a WINE-powered Excel or similar).

CSV (Comma Separated Values) is simply a flat text file. Each row is contained on a line, each cell separated by a comma. ie
,Name,Age,Phone Number
1,Jack,23,555-1234
2,John,34,555-4321
3,Jimmy,21,555-5454
etc...
*Note the first ',' simply will place a blank cell when you open it in Excel because it splits the lines by comma and finds a NULL value for the first element, thereby creating a blank cell. (Try saving those contents as test.csv and opening in Excel to see for yourself)

The benefit to doing this is that you can work with it just like any other file object in Python.
Jun 20 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.