word裡面有2張表,需要找到第二張表,並寫入execl中: 代碼如下: 運行後生成文件 roro.xlsx,內容如下: ...
word裡面有2張表,需要找到第二張表,並寫入execl中:
代碼如下:
#coding:utf-8 import os from docx import Document import win32com from win32com.client import Dispatch, constants def parse_docx(f,title): d = Document(f) for t in d.tables: '''獲取需要的表''' tbTitle = t.cell(0, 0).text if title == tbTitle: tableInfo = [] columnLen = len(t.columns) rowLen = len(t.rows) for i in range(0,columnLen): tmp = [] for row in t.rows: tmp.append(row.cells[i].text) #刪除第一個元素->表名 del(tmp[0]) tableInfo.append(tmp) #返回的後兩個參數表示tableInfo表的行數和列數 return [tbTitle,tableInfo,rowLen-1,columnLen] return None def writeExecl(fileName,sheet,tableInfo): excel = win32com.client.Dispatch('Excel.Application') excel.Visible=0 excel.DisplayAlerts=0 #對傳入文件名的處理 if fileName: if os.path.exists(fileName): workbook = excel.Workbooks.Open(fileName) else: workbook = excel.Workbooks.Add() workbook.SaveAs(fileName) else: workbook = excel.Workbooks.Add() try: sht = workbook.Worksheets(sheet) except: sheetNew = workbook.Worksheets.Add() sheetNew.Name =sheet sheetNew.Activate() sht = workbook.Worksheets(sheet) #execl表格是從1開始的 sht.Cells(1, 1).Value = tableInfo[0] #把tableInfo看作是一行數據,依次賦值 for i in range(0,tableInfo[3]): for j in range(0,tableInfo[2]): sht.Cells(j+2, i+1).Value = tableInfo[1][i][j] workbook.Save() excel.Application.Quit() if __name__ == "__main__": docxFile = "123.docx" execlFile = "roro.xlsx" sheet = "roro" tableName = "內科" #讀取word中tableName的內容 tableInfo = parse_docx(docxFile,tableName) #處理execl writeExecl(execlFile,sheet,tableInfo)
運行後生成文件 roro.xlsx,內容如下: