Last active
September 15, 2015 20:43
-
-
Save jgstew/395cdf96ca1695385adb to your computer and use it in GitHub Desktop.
Automating Excel with Python using Win32com interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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