[python-win32] Excel Document
Michiel Overtoom
motoom at xs4all.nl
Fri Jul 25 12:52:04 CEST 2008
Sina wrote...
>To read the contents of an excel spreadsheet. In one case I am trying to
>read the value of the cell rather than its contents. i.e. if I have a
>hyperlink called "Document" or "Procedure" which is a hyperlink to
>http://intranet/foo.pdf then this script will return "Document" or "Link" as
>a unicode string rather than the URL that I require.
Could you post an example of such a worksheet somewhere?
>I would also like to be able to read other cell attributes such as colour,
>if possible, but this is not essential.
This works for me. It uses an example sheet, which should be in the same
directory as from you run the script from, you can get at
ftp://ftp.motoom.net/files/excel.xls
# excel.py:
import win32com.client
import os
xlsname=os.path.join(os.getcwd(),"excel.xls")
excel=win32com.client.DispatchEx("Excel.Application")
book=excel.Workbooks.Open(xlsname)
sheet=book.Sheets(1)
rowcount=sheet.UsedRange.Rows.Count
colcount=sheet.UsedRange.Columns.Count
for row in range(1,rowcount+1):
for col in range(1,colcount+1):
cell=sheet.Cells(row,col)
if not cell.Value: continue # skip empty cells
print "\nRow",row,"Col",col
if cell.Value: print "\tValue:",cell.Value
if cell.HasFormula: print "\tFormula:",cell.Formula
if cell.Font.ColorIndex>0: print "\tColor:",cell.Font.ColorIndex
if cell.Font.FontStyle!="Regular": print "\tStyle:",cell.Font.FontStyle
excel=None
--
"The ability of the OSS process to collect and harness
the collective IQ of thousands of individuals across
the Internet is simply amazing." - Vinod Vallopillil
http://www.catb.org/~esr/halloween/halloween4.html
More information about the python-win32
mailing list