Entry
How can I control MS-Access from Python?
Mar 2nd, 2000 15:02
Nigel Linnett, unknown unknown, Nathan Wallace, http://starship.python.net/crew/mhammond/win32/Downloads.html
This simple code depends on having Mark Hammond's win32-all extension
installed.
For the best functionality run MakePy (In the tools menu of Pythonwin)
on all Microsoft Excel Object Libraries.
###### Begin code here
# This code is available for you to use as you see fit
# Tested with Python 1.5.2 and Excel 97 and Excel 2000
# No Problems were apparent, but this does not mean there are none
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)
#
###### Code Ends here
Hope this helps you out a bit, it works a lot better if you run makepy
on the excel object libraries you will find.