700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > python读取word中后缀名docx的文件的表格

python读取word中后缀名docx的文件的表格

时间:2024-03-14 10:17:51

相关推荐

python读取word中后缀名docx的文件的表格

1.安装所需要的解析包

pip install python-docx

pip install docx

2.使用代码例子来进行演示用法

解析出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': '江房售许字()第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': ''}]}'''

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。