python 生成 ppt word outmail excel

xiaoxiao2021-02-28  10

# coding:utf-8 from Tkinter import Tk from time import sleep from tkMessageBox import showwarning import win32com.client as win32 from time import sleep, ctime from urllib import urlopen warn = lambda app: showwarning(app, 'Exit?') RANGE = range(3, 10) def excel(): s='''I wasn't really shot with a silver bullet.''' sss = s.split() app = 'Excel' xl = win32.gencache.EnsureDispatch('%s.Application' % app) ss = xl.Workbooks.Add() sh = ss.ActiveSheet xl.Visible = True sleep(0.5) sh.Cells(1, 1).Value = 'Python-to-%s Demo' % app sleep(0.5) ii = 2 for i in sss: print i sh.Cells(ii, 1).Value = i ii += 1 sleep(0.5) sh.Cells(ii+2, 1).Value = "xixihahaha" warn(app) ss.Close(False) xl.Application.Quit() 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\r\n\r\n' % app) sleep(1) for i in range(3, 8): rng.InsertAfter('Line %d\r\n' % i) sleep(1) rng.InsertAfter('\r\nTh-th-th-thats all folks!') warn(app) doc.Close(False) word.Application.Quit() def ppoint(): app = 'PowerPoint' ppoint = win32.gencache.EnsureDispatch('%s.Application' % app) pres = ppoint.Presentations.Add() ppoint.Visible = True s1 = pres.Slides.Add(1, win32.constants.ppLayoutText) sleep(1) s1a = s1.Shapes[0].TextFrame.TextRange s1a.Text = 'Python-to-%s Test\r\n\r\n' % app sleep(1) s1b = s1.Shapes[1].TextFrame.TextRange for i in range(3, 8): s1b.InsertAfter('Line %d\r\n' % i) sleep(1) s1b.InsertAfter("\r\nTh-th-th-thats all folks!") warn(app) pres.Close(False) ppoint.Quit() def outlook(): app = 'Outlook' olook = win32.gencache.EnsureDispatch('%s.Application' % app) mail = olook.CreateItem(win32.constants.olMailItem) recip = mail.Recipients.Add('815467444@qq.com') subj = mail.Subject = 'Python windows program demo with outlook\n' body = ['Line %d' % i for i in RANGE] body.insert(0, '%s\r\n' % subj) body.append("\r\nTh-th-th-that's all folks!") mail.Body = '\r\n'.join(body) mail.Send() ns = olook.GetNamespace("MAPI") obox = ns.GetDefaultFolder(win32.constants.olFolderOutbox) obox.Display() obox.Items.Item(1).Display() warn(app) olook.Quit() TICKS = ('BABA', 'GOOG', 'EBAY', 'AMZN') COLS = ('TICKER', 'PRICE', 'CHG', '%AGE') URL = 'http://quote.yahoo.com/d/quotes.csv?s=%s&f=sl1c1p2' def excell(): 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 Stock quote demo' % app sleep(1) sh.Cells(3, 1).Value = 'Prices quoted as of: %s' % ctime() sleep(1) for i in range(4): sh.Cells(5, i+1).Value = COLS[i] sleep(1) sh.Range(sh.Cells(5, 1), sh.Cells(5, 4)).Font.Bold = True sleep(1) row = 6 u = urlopen(URL % ','.join(TICKS)) for data in u: tick, price, chg, per = data.split(',') sh.Cells(row, 1).Value = eval(tick) sh.Cells(row, 2).Value = ('%.2f' % round(float(price), 2)) sh.Cells(row, 3).Value = chg sh.Cells(row, 4).Value = eval(per.rstrip()) row += 1 sleep(1) u.close() warn(app) ss.Close(False) xl.Application.Quit() if __name__ == '__main__': Tk().withdraw() excell() #excel() #word() #ppoint() # for i in range(20): # outlook() # sleep(5) # print i
转载请注明原文地址: https://www.6miu.com/read-1750014.html

最新回复(0)