from tkinter import Tk from time import sleep from tkinter.messagebox import showwarning import win32com.client as win32 warn=lambda app:showwarning(app,'Exit?') RANGE=range(3,8) def excel(): app='Excel' xl=win32.gencache.EnsureDispatch('%s.Application'%app) ss=xl.Workbooks.Add() sh=ss.ActiveSheet xl.Visible=True sleep(1) sh.Cells(1,1).Value='Python-to-%s Demo'%app sleep(1) for i in RANGE: sh.Cells(i,1).Value='Line %d'%i sleep(1) sh.Cells(i+2,1).Value="Th-th-th-that's all folks!" warn(app) ss.Close(False) xl.Application.Quit() if __name__=='__main__': Tk().withdraw() excel()
摘自:python核心编程(第三版)
from tkinter import Tk from time import sleep from tkinter.messagebox import showwarning import win32com.client as win32 warn=lambda app: showwarning(app,'Exit?') RANGE=range(3,8) def word(): app='word' word=win32.gencache.EnsureDispatch('%s.Application'%app) doc=word.Documents.Add() word.Visible=True sleep(1) rng=doc.Range(0,0) rng.InsertAfter('Python-to-%s Test '%app) sleep(1) for i in RANGE: rng.InsertAfter('Line %d '%i) sleep(1) rng.InsertAfter(" Th-th-th-that's all folks! ") warn(app) doc.Close(False) word.Application.Quit() if __name__=='__main__': Tk().withdraw() word()