参照《Python核心编程》练习遇到问题3个。
1、增加#coding:utf-8
2、一直提示没有Cell对象。查看报错源文件,搜索Cell发现只有Cells,确定代码少敲了s.低级错误。
sh.Cells(1,1).Value = 'Python-to-%s Demo'%app
3、showwarning时,提示框未显示在屏幕最前端。(未解决)
#coding:utf-8
#!/usr/bin/env python
from Tkinter import Tk
from time import sleep
from tkMessageBox import showwarning
#from Tkinter import messagebox
import win32com.client as win32
warn = lambda app:showwarning(app,'Exit?')
#warn = lambda app:messagebox.askokcancel(app,'Exit?')
RANGE = range(3,8)
def excel():
app ='Excel'
x1 = win32.gencache.EnsureDispatch('%s.Application'%app)
ss = x1.Workbooks.Add()
sh = ss.ActiveSheet
x1.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)
# print "warning",showwarning("Excel","exit?")
ss.Close(False)
x1.Application.Quit()
if __name__=='__main__':
Tk().withdraw()
excel()