1. 程式人生 > >python讀取word文件表格裡的資料

python讀取word文件表格裡的資料

首先需要安裝相應的支援庫:
直接在命令列執行pip install python-docx

示例程式碼如下:

import docx
from docx import Document #匯入庫

path = "E:\\python_data\\1234.docx" #檔案路徑
document = Document(path) #讀入檔案
tables = document.tables #獲取檔案中的表格集
table = tables[0  ]#獲取檔案中的第一個表格
for i in range(1,len(table.rows)):#從表格第二行開始迴圈讀取表格資料
    result = table.cell
(i,0).text + "" +table.cell(i,1).text+ table.cell(i,2).text + table.cell(i,3).text #cell(i,0)表示第(i+1)行第1列資料,以此類推 print(result)