回覆列表
  • 1 # 可可的大山

    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()

  • 中秋節和大豐收的關聯?
  • 四年級下冊作文《走在放學回家的路上》700字數?