[日常]用Python读取word文档中的表格并比较

最近想对某些word文档(docx)的表格内容作比较, 于是找了一下相关工具. 参考Automate the Boring Stuff with Python中的word部分, 试用了python-docx – python-docx 0.8.7 documentation

演示如下. 两个简单的word文档, 各有一个表格:

《[日常]用Python读取word文档中的表格并比较》

读取文档中的表格到列表(为演示只对单列表格操作):

import docx

def 取表格(文件名):
  文件 = docx.Document(文件名)
  首个表 = 文件.tables[0]
   = []
  for  in 首个表.rows:
    for  in .cells:
      .append(.text)
  print(文件名 + " -> " + str())
  return 
    
表1 = 取表格('表1.docx')

读取结果:

表1.docx -> ['值1', '值2', '值3']

接着找到这个做比较的python库seperman/deepdiff, 来源: Get difference between two lists

from deepdiff import DeepDiff

表1 = 取表格('表1.docx')
表2 = 取表格('表2.docx')

print(DeepDiff(表1, 表2))

输出结果(为更可读, 已手动格式化):

{
'values_changed': 
  {'root[1]': 
    {'new_value': '值2.5', 'old_value': '值2'}
  }, 
'iterable_item_added': 
  {'root[3]': '值4'}
}

显示了修改的值和添加的值, 还挺好用. 实际的表格是两列, 需要按照某个键值作对比. 于是用字典, 正好DeepDiff也提供两个字典间的比较. 双列表文件演示:

《[日常]用Python读取word文档中的表格并比较》
《[日常]用Python读取word文档中的表格并比较》

读取双列表到字典后, 进行比较:

import docx
from deepdiff import DeepDiff
from pprint import pprint

def 取表格(文件名):
  文件 = docx.Document(文件名)
  首个表 = 文件.tables[0]
   = {}
  for  in 首个表.rows:
     = .cells
    [[0].text] = [1].text
  print(文件名 + " -> " + str())
  return 

表1 = 取表格('双列表1.docx')
表2 = 取表格('双列表2.docx')

pprint(DeepDiff(表1, 表2), indent=2)

输出如下:

{ 'dictionary_item_added': {"root['键3']"},
  'values_changed': {"root['键2']": {'new_value': '值2.5', 'old_value': '值2'}}}

源码在: program-in-chinese/house_of_10000_business

    原文作者:吴烜
    原文地址: https://zhuanlan.zhihu.com/p/45997055
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞