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': '江房售许字(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': ''}]}
'''

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

还是那个同伟伟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值