欧美一区二区三区,国内熟女精品熟女A片视频小说,日本av网,小鲜肉男男GAY做受XXX网站

Python使用pywin32操作Office

錢淋西2年前16瀏覽0評論

Python使用pywin32操作Office?

pywin32操作office要通過com接口,office(排除365)并沒有直接的接口(排除VBA)可以開發完成自動化操作。

Word的com文檔:

https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.word?view=word-pia

Excel:

https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel?view=excel-pia

其他的可以在這里搜索:

https://docs.microsoft.com/en-us/dotnet/api/index?term=Microsoft.Office.Interop

兩個簡單的例子

Python 簡單操作Excel

import time

import win32com.client as win32

#----------------------------------------------------------------------

def excel():

""""""

xl = win32.gencache.EnsureDispatch('Excel.Application')

ss = xl.Workbooks.Add()

sh = ss.ActiveSheet

xl.Visible = True

time.sleep(1)

sh.Cells(1,1).Value = 'Hacking Excel with Python Demo'

time.sleep(1)

for i in range(2,8):

sh.Cells(i,1).Value = 'Line %i' % i

time.sleep(1)

ss.Close(False)

xl.Application.Quit()

if __name__ == "__main__":

excel()

Python簡單操作Word

from time import sleep

import win32com.client as win32

RANGE = range(3, 8)

def word():

word = win32.gencache.EnsureDispatch('Word.Application')

doc = word.Documents.Add()

word.Visible = True

sleep(1)

rng = doc.Range(0,0)

rng.InsertAfter('Hacking Word with Python\r\n\r\n')

sleep(1)

for i in RANGE:

rng.InsertAfter('Line %d\r\n' % i)

sleep(1)

rng.InsertAfter("\r\nPython rules!\r\n")

doc.Close(False)

word.Application.Quit()

if __name__ == '__main__':

word()