使用Python将数据从Word表导出到Excel

时间:2017-06-22 19:48:42

标签: python database excel ms-word

我有成千上万的word文档,第一页上有一个表,我需要输出到Excel中的信息。到目前为止,我有这个,但我不确定为什么它不起作用

import win32com.client as win32
import os

myDir = r'C:\Projects\Capital'


XL = win32.Dispatch('Excel.Application')
XL.Visible = 1
XLbook = XL.Workbooks.Open(os.path.join(myDir,'Dealflow.xlsx'))
XLsheet = XLbook.Worksheets(1)
XLrow = 2

for myFile in os.listdir(myDir):
    filepath = os.path.join(myDir,myFile)
    filename = os.path.splitext(myFile)[0]
    ext = os.path.splitext(myFile)[1]
    if ext == '.docx':
        word = win32.Dispatch('Word.Application')
        word.Visible = 1
        word.Documents.Open(filepath)
        doc = word.ActiveDocument
        table = doc.Tables(1)
        XLsheet.Cells(XLrow,1).Value = table.Cell(Row=1, Column=1).Range.Text
        XLsheet.Cells(XLrow,2).Value = table.Cell(Row=2, Column=3).Range.Text
        XLrow = XLrow +1
        doc.Close()

0 个答案:

没有答案