Skip to content

Instantly share code, notes, and snippets.

@jgstew
Last active September 15, 2015 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgstew/395cdf96ca1695385adb to your computer and use it in GitHub Desktop.
Save jgstew/395cdf96ca1695385adb to your computer and use it in GitHub Desktop.
Automating Excel with Python using Win32com interface
import time
import random
# win32com does not come with python
from win32com.client import Dispatch
xlApp = Dispatch("Excel.Application")
xlApp.Visible = 1
xlApp.Workbooks.Add()
# http://web.archive.org/web/20090916091434/http://www.markcarter.me.uk/computing/python/excel.html
# http://stackoverflow.com/questions/510348/how-can-i-make-a-time-delay-in-python
time.sleep(.5)
xlApp.ActiveSheet.Cells(11,1).Value = 'Total:'
xlApp.ActiveSheet.Cells(11,2).Value = '=SUM(B1:B10)'
xlApp.ActiveSheet.Cells(12,1).Value = 'Average:'
xlApp.ActiveSheet.Cells(12,2).Value = '=AVERAGE(B1:B10)'
time.sleep(.5)
# https://wiki.python.org/moin/ForLoop
for x in range(1, 11):
time.sleep(.5)
rand = random.randrange(100)
print rand
xlApp.ActiveSheet.Cells(x,2).Value = rand
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment