473,396 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to operate the excel by python?

i want to compare the content in excel,but i don't know whick module to use!
can you help me?

Jul 19 '05 #1
6 2462
The key is Python for Windows :
http://starship.python.net/crew/mhammond/win32/

See here for an Excel dispatch example:
http://aspn.activestate.com/ASPN/Coo.../Recipe/325735

When doing such operations, I generally save all the Excel files to CSV
files and do the operations on them using the csv module.

Jul 19 '05 #2
ÒÊÃÉɽÈË wrote:
i want to compare the content in excel,but i don't know whick module to use!
can you help me?


I noticed a package on PyPi today that might be useful to you:

http://www.python.org/pypi/xlrd/0.3a1

The homepage is a little brief, so I clipped their example from the
README:

import xlrd
book = xlrd.open_workbook("myfile.xls")
print "The number of worksheets is", book.nsheets
print "Worksheet name(s):", book.sheet_names()
sh = book.sheet_by_index(0)
print sh.name, sh.nrows, sh.ncols
print "Cell D30 is", sh.cell_value(rowx=29, colx=3)
for rx in range(sh.nrows):
print sh.row(rx)

I haven't had cause to use it myself, however.

-alex23

Jul 19 '05 #3
Rune Strand wrote:
The key is Python for Windows :
http://starship.python.net/crew/mhammond/win32/

See here for an Excel dispatch example:
http://aspn.activestate.com/ASPN/Coo.../Recipe/325735

When doing such operations, I generally save all the Excel files to CSV
files and do the operations on them using the csv module.


Problems with that approach:

1. Unfortunately, "save as CSV" is very much a WYSIWYG operation. If the
"number formats" are not sensible, loss of information can result.

Example: user gets (text) data file which needs extra columns added.
User loads it into Excel, adds extra data, saves as csv. One column is
an identifier which just happens to be numeric -- say a 12-digit credit
card number 123456789012. The user doesn't care that this is showing on
the Excel screen as 1.23457E+11 (default format) as he is using the
6-digit customer account number 654321 to find the extra data he needs
to add. He may not even see the 1.23457E+11 because it's in column BQ
and he's inserting 3 columns in front of column E.

To avoid that problem, one has to check all the columns and reformat
those that do not display all the data. This is not something that a
user can be relied on to do, even when stated clearly in a procedure manual.

2. The tedium and error-proneness of "saving as": (a) you get given a
file named "fubar.csv" but it was "fubar.csv.xls" before the user
renamed it. (b) Excel files can have multiple worksheets, which have to
be saved each as a separate csv file.

Consequently, an approach which reads the .XLS file directly has
attractions.

One such approach, which unlike the COM approach doesn't need Excel to
be on the reading machine, and doesn't even need Windows, is the free
"xlrd" module [of which I am the author] -- see
http://www.lexicon.net/sjmachin/xlrd.htm
or
http://www.python.org/pypi/xlrd/

Regards,
John
Jul 19 '05 #4
John,

I wrote a script that autmates the conversion from XLS to CSV. It's
easy. But your points are still good. Thanks for making me aware the
xlrd module!

Jul 19 '05 #5
John Machin <sj******@lexicon.net> wrote:
Rune Strand wrote:
The key is Python for Windows :
http://starship.python.net/crew/mhammond/win32/

See here for an Excel dispatch example:
http://aspn.activestate.com/ASPN/Coo.../Recipe/325735

When doing such operations, I generally save all the Excel files to CSV
files and do the operations on them using the csv module.


Problems with that approach:

1. Unfortunately, "save as CSV" is very much a WYSIWYG operation. If the
"number formats" are not sensible, loss of information can result.


This is a real problem. US postal codes are a particular nasty issue. The
value "01234", for example, will be imported into Excel as "1234".
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 19 '05 #6
> i want to compare the content in excel,but i don't know whick module to use!
can you help me?

Read "Python programming on Win32" book, use win32api module. According
to Chad's advices about Excel (VBA) constants, you can find following
link usefull as well.
http://fox.wikis.com/wc.dll?Wiki~ExcelConstants~VFP

HTH

Petr

Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Allison Bailey | last post by:
Hi Folks, I'm a brand new Python programmer, so please point me in the right direction if this is not the best forum for this question.... I would like to open an existing MS Excel spreadsheet...
4
by: Marc | last post by:
Hi all, I am trying to write an application where I need the ability to open an Excel spreadsheet and do basic read/write, insert rows, and hide/unhide rows. Using win32com I have been able to...
5
by: Bram Stolk | last post by:
pyHello, Google helped me find a nice package to interface python with MS Excel. "win32all", or sometimes called by the name "pywin32". http://starship.python.net/crew/mhammond/ However,...
2
by: Kevin T. Ryan | last post by:
Hi Group - I have written a "semi-program" in MS Excel related to running a football pool. I've updated it over the past two years or so, to the point where it is getting pretty advanced. ...
3
by: Xiao-Qin Xia | last post by:
I don't like VBA, and want to use python to work with Excel. Does anybody recommend some good documents to begin with? Many thanks, Xiao-Qin
10
by: Robert Hicks | last post by:
I need to pull data out of Oracle and stuff it into an Excel spreadsheet. What modules have you used to interface with Excel and would you recommend it? Robert
4
by: michael.pearmain | last post by:
Hi Experts, Looking for a very quick bit on of advice on how to make some python code run. I'm a newbie to both VBA and Python, so i apologise if this is very easy but i'm about to tear my hair...
6
by: AleydisGP | last post by:
I have a .plt file (which is a tab delimited ASCII file) and I want to format it to get a .dbf with data in rows and columns, detele some rows/columns and substitute decimal '.' with ','. All this...
15
by: patf | last post by:
Hi - experienced programmer but this is my first Python program. This URL will retrieve an excel spreadsheet containing (that day's) msci stock index returns. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.