python讀取word 中指定位置的表格及表格數(shù)據(jù)
1.Word文檔如下:

2.代碼
# -*- coding: UTF-8 -*-
from docx import Document
def readSpecTable(filename, specText):
document = Document(filename)
paragraphs = document.paragraphs
allTables = document.tables
specText = specText.encode('utf-8').decode('utf-8')
for aPara in paragraphs:
if aPara.text == specText:
ele = aPara._p.getnext()
while (ele.tag != '' and ele.tag[-3:] != 'tbl'):
ele = ele.getnext()
if ele.tag != '':
for aTable in allTables:
if aTable._tbl == ele:
for i in range(len(aTable.rows)):
for j in range(len(aTable.columns)):
print(aTable.cell(i, j).text)
if __name__ == '__main__':
readSpecTable('test.docx', '符號約定')
3.結果
符號
符號
含義
數(shù)據(jù)域取值符號
M
必須填寫的域
數(shù)據(jù)域取值符號
C
某條件成立時必須填寫的域
數(shù)據(jù)域取值符號
O
可選,非必須填寫的域
數(shù)據(jù)域取值符號
←
必須與先前報文中對應域的值相同的域
數(shù)據(jù)域取值符號
-
必須去除的域
數(shù)據(jù)域屬性符號
基本數(shù)據(jù)域
數(shù)據(jù)域屬性符號
[]
標識為消息組件名稱數(shù)據(jù)域
數(shù)據(jù)域屬性符號
{}
標識為消息組件中重復的數(shù)據(jù)域
數(shù)據(jù)域屬性符號
→
標識為消息組件中包含的基礎數(shù)據(jù)域
數(shù)據(jù)域屬性符號
→[]
標識為消息組件中的子消息組件
數(shù)據(jù)域屬性符號
→{}
標識為子消息組件中重復的數(shù)據(jù)域塊
數(shù)據(jù)域屬性符號
→→
標識為子消息組件中包含的基礎數(shù)據(jù)域
PS:python讀取word文檔表格里的數(shù)據(jù)
首先需要安裝相應的支持庫:
直接在命令行執(zhí)行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)):#從表格第二行開始循環(huán)讀取表格數(shù)據(jù) 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列數(shù)據(jù),以此類推 print(result)
總結
以上所述是小編給大家介紹的python讀取word 中指定位置的表格及表格數(shù)據(jù),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
- python 刪除excel表格重復行,數(shù)據(jù)預處理操作
- Python3讀取和寫入excel表格數(shù)據(jù)的示例代碼
- 基于Python快速處理PDF表格數(shù)據(jù)
- Python基于pandas爬取網(wǎng)頁表格數(shù)據(jù)
- 基于python實現(xiàn)把json數(shù)據(jù)轉換成Excel表格
- 使用 Python 讀取電子表格中的數(shù)據(jù)實例詳解
- python 中Arduino串口傳輸數(shù)據(jù)到電腦并保存至excel表格
- Python 用三行代碼提取PDF表格數(shù)據(jù)
- Python獲取數(shù)據(jù)庫數(shù)據(jù)并保存在excel表格中的方法
- python 獲取頁面表格數(shù)據(jù)存放到csv中的方法
- python3 讀取Excel表格中的數(shù)據(jù)
- 利用python做表格數(shù)據(jù)處理
相關文章
配置python連接oracle讀取excel數(shù)據(jù)寫入數(shù)據(jù)庫的操作流程
這篇文章主要介紹了配置python連接oracle,讀取excel數(shù)據(jù)寫入數(shù)據(jù)庫,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
python kmeans聚類簡單介紹和實現(xiàn)代碼
這篇文章主要為大家詳細介紹了python kmeans聚類簡單介紹和實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-02-02
如何將yolo格式轉化為voc格式:txt轉xml(親測有效)
這篇文章主要介紹了如何將yolo格式轉化為voc格式:txt轉xml,親測有效,可以使用,本文通過圖文并茂的形式給大家介紹的非常詳細,感興趣的朋友參考下吧2023-12-12
linux mint中搜狗輸入法導致pycharm卡死的問題
這篇文章主要介紹了linux mint中搜狗輸入法導致pycharm卡死的問題,這篇文章給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10
Python 完美解決 Import “模塊“ could not&n
這篇文章主要介紹了Python 完美解決 Import “模塊“ could not be resolved ...,本文給大家分享問題原因及解決方法,需要的朋友可以參考下2022-11-11

