Microsoft Officeプログラミング(一)
26350 ワード
1.概要
Pythonを使用してCOMクライアントのプログラミングを行うことで、Word、Excel、PowerPoint、OutlookなどのMicrosoft Officeアプリケーションを制御し、通信することができます.COMは、PCアプリケーションを用いて他のアプリケーションと対話できるサービスである.
従来の意味では、COMクライアントは一般的にVB/VBCとC++の2つの異なるツールを使用して作成されています.COMプログラミングにとって、Pythonは一般的にVBよりも強く、C++開発よりも表現力が高く、時間消費が少ないため、実行可能な代替品と見なされています.
2.PythonによるCOMクライアントプログラミングインタラクションの基本ステップ アプリケーション を起動適切な文書を追加する(または既存の文書をロードする) .アプリケーションを表示する ドキュメントの実行に必要なすべての作業 ドキュメントを保存または破棄する 終了 Excel
抜粋:pythonコアプログラミング(第3版) Word PowerPoint Outlook
Pythonを使用してCOMクライアントのプログラミングを行うことで、Word、Excel、PowerPoint、OutlookなどのMicrosoft Officeアプリケーションを制御し、通信することができます.COMは、PCアプリケーションを用いて他のアプリケーションと対話できるサービスである.
従来の意味では、COMクライアントは一般的にVB/VBCとC++の2つの異なるツールを使用して作成されています.COMプログラミングにとって、Pythonは一般的にVBよりも強く、C++開発よりも表現力が高く、時間消費が少ないため、実行可能な代替品と見なされています.
2.PythonによるCOMクライアントプログラミング
抜粋:pythonコアプログラミング(第3版)
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from tkinter import Tk
import time
from tkinter.messagebox import showwarning
import win32com.client as win32
# ,
'''
def warn(app):
showwarning(app,'Exit?')
'''
warn=lambda app:showwarning(app,'Exit?')
RANGE=range(3,8)
def excel():
app='Excel'
'''
,PythonWin Makepy ,
:x1=win32.Dispatch('{0}.Application'.format(app))
'''
x1=win32.gencache.EnsureDispatch('{0}.Application'.format(app))
# sheet
ss=x1.Workbooks.Add()
# sheet
sh=ss.ActiveSheet
#
x1.Visible=True
time.sleep(1)
# (1,1)
sh.Cells(1,1).Value='Python-to-{0} Demo'.format(app)
time.sleep(1)
for i in RANGE:
sh.Cells(i,1).Value='Line {0}'.format(i)
time.sleep(1)
sh.Cells(i+2,1).Value="Th-th-th-that's all folks!"
#
warn(app)
#
ss.Close(False)
x1.Application.Quit()
if __name__ == '__main__':
# Tk ,
Tk().withdraw()
excel()
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from tkinter import Tk
import time
from tkinter.messagebox import showwarning
import win32com.client as win32
# ,
'''
def warn(app):
showwarning(app,'Exit?')
'''
warn=lambda app:showwarning(app,'Exit?')
RANGE=range(3,8)
def word():
app='Word'
'''
,PythonWin Makepy ,
:x1=win32.Dispatch('{0}.Application'.format(app))
'''
word=win32.gencache.EnsureDispatch('{0}.Application'.format(app))
# doc
doc=word.Documents.Add()
#
word.Visible=True
time.sleep(1)
# Range ,
rng=doc.Range(0,0)
#
rng.InsertAfter('Python-to-{0} Test\r
\r
'.format(app))
time.sleep(1)
for i in RANGE:
rng.InsertAfter('Line {0}\r
'.format(i))
time.sleep(1)
rng.InsertAfter("\r
Th-th-th-that's all folks!\r
")
#
warn(app)
#
doc.Close(False)
word.Application.Quit()
if __name__ == '__main__':
# Tk ,
Tk().withdraw()
word()
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from tkinter import Tk
import time
from tkinter.messagebox import showwarning
import win32com.client as win32
# ,
'''
def warn(app):
showwarning(app,'Exit?')
'''
warn=lambda app:showwarning(app,'Exit?')
RANGE=range(3,8)
def ppoint():
app='PowerPoint'
'''
,PythonWin Makepy ,
:x1=win32.Dispatch('{0}.Application'.format(app))
'''
ppoint=win32.gencache.EnsureDispatch('{0}.Application'.format(app))
#
pres=ppoint.Presentations.Add()
#
ppoint.Visible=True
#
s1=pres.Slides.Add(1,win32.constants.ppLayoutText)
time.sleep(1)
'''
ps: python3 dict.keys , dict_keys ,
s1a=s1.Shapes[0].TextFrame.TextRange
s1a=list(s1.Shapes)[0].TextFrame.TextRange
'''
#shape[0] ppLayoutTitle, title
s1a=list(s1.Shapes)[0].TextFrame.TextRange
s1a.Text='Python-to-{0} Demo'.format(app)
time.sleep(1)
#shape[1] ppLayoutText, Text
s1b = list(s1.Shapes)[1].TextFrame.TextRange
for i in RANGE:
s1b.InsertAfter('Line {0}\r
'.format(i))
time.sleep(1)
s1b.InsertAfter("\r
Th-th-th-that's all folks!")
#
warn(app)
#
pres.Close()
ppoint.Quit()
if __name__ == '__main__':
# Tk ,
Tk().withdraw()
ppoint()
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from tkinter import Tk
import time
from tkinter.messagebox import showwarning
import win32com.client as win32
# ,
'''
def warn(app):
showwarning(app,'Exit?')
'''
warn=lambda app:showwarning(app,'Exit?')
RANGE=range(3,8)
def outlook():
app='Outlook'
'''
,PythonWin Makepy ,
:x1=win32.Dispatch('{0}.Application'.format(app))
'''
olook=win32.gencache.EnsureDispatch('{0}.Application'.format(app))
#
mail=olook.CreateItem(win32.constants.olMailItem)
#
recip=mail.Recipients.Add('[email protected]')
#
subj=mail.Subject='Python-to-{0} Demo'.format(app)
#
body=['Line {0}'.format(i) for i in RANGE]
body.insert(0,'{0}\r
'.format(subj))
body.append("\r
Th-th-th-that's all folks!")
mail.body='\r
'.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()
if __name__ == '__main__':
# Tk ,
Tk().withdraw()
outlook()