Python accessing Excel 97 via com
Calishar
calishar at *NOSPAM*home.com
Thu Feb 24 19:36:06 EST 2000
On Thu, 24 Feb 2000 14:48:03 GMT, iam at not.you (Ken Power) wrote a
summary of his Excel COM attempts.
<posted and emailed, my posts don't seem to post everywhere>
<all snipped>
Hi Ken,
This is how I tend to do Excel stuff (tested and working)
import win32com.client
# Launch Excel and make it visible
ExcelApp=win32com.client.Dispatch("Excel.Application")
ExcelApp.visible = 1
# Add a new workbook to the collection (Excel doesn't always start up
# with a workbook open)
workbook = ExcelApp.Workbooks.Add()
# Add a worksheet to the current workbook
worksheet=workbook.Worksheets.Add()
# Do two simple nested loops
for row in range(1,10):
for column in range(1,10):
# Get a reference to the current cell
cell = worksheet.Cells(row, column)
# Create a value
val = "Row %i, Col %i" % (row, column)
# Set the cell Value
# Use cell.formula to set a formula
cell.Value=val
#Do some fancy stuff
cell.Font.Bold=1
# Loop through all the columns we just used
for column in range(1,10):
# Set the width appropriately
worksheet.Cells(1,column).ColumnWidth=12
# Save the workbook in the default location
workbook.SaveAs("exceldemo.xls")
# Quit Excel
ExcelApp.Quit()
# Then delete all the objects we just used
del(cell)
del(worksheet)
del(workbook)
del(ExcelApp)
# End of application
Hope this helps you out a bit, it works a lot better if you run makepy
on the excel object libraries you will find.
Calishar
More information about the Python-list
mailing list