python读取word中后缀名docx的文件的表格
生活随笔
收集整理的這篇文章主要介紹了
python读取word中后缀名docx的文件的表格
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.安裝所需要的解析包
pip install python-docx pip install docx2.使用代碼例子來進行演示用法
解析出word中的如下表格:
?
def parse_docx(file):word_docx = docx.Document(file)table = word_docx.tables[0] # 讀取word中第一個表格type_list = []for i in range(2, len(table.rows)): # 從第三行開始讀取purpose = table.cell(i, 2).text # 用途 取一行當中的第二個total = table.cell(i, 3).text # 套數 取一行當中的第三個area = table.cell(i, 4).text # 面積 取一行當中的第四個type_list.append({"buildingType": purpose,"total": total, # 套數"buildingArea": area + "平方米",})documentNumber = table.cell(2, 0).text # 預售證號 # 取第二行的第一個address = table.cell(2, 1).text # 坐落 # 取第二行的第二個pro_info = {"documentNumber": documentNumber,"address": address,"type": type_list}return pro_info ''' 注意:前兩列有合并的,下面解析出來的也是一樣的數據 打印結果 {'address': '江山市賀村鎮賀溪路與中心南街交匯處1-6號、11-17號、22-26號及10、30、33、34、35、37幢','documentNumber': '江房售許字(2021)第ZJ00059號','projectName': ('東旺賀悅小區1-6號、11-17號、22-26號及10、30、33、34、35、37幢',),'type': [{'buildingArea': '18502.16平方米','buildingType': '成套住宅','total': '208'},{'buildingArea': '838.06平方米', 'buildingType': '商業', 'total': '18'},{'buildingArea': '3694.70平方米', 'buildingType': '住宅', 'total': '18'},{'buildingArea': '平方米', 'buildingType': '', 'total': ''}]} '''總結
以上是生活随笔為你收集整理的python读取word中后缀名docx的文件的表格的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python中使用代码将后缀名doc文件
- 下一篇: Python中读取打开文件的各种模式以及