Python中docx2txt库的使用说明

作者:angus_monroe 时间:2022-03-23 18:58:46 

docx2txt的Github地址

docx2txt是基于python的从docx文件中提取文本和图片的库。

代码是从python-docx中获取的。它也可以从页眉,页脚和超链接中提取文本。它现在也可以提取图像。

安装


pip install docx2txt

运行

1、命令行运行


# extract text
docx2txt file.docx
# extract text and images
docx2txt -i /tmp/img_dir file.docx

2、在python中调用


# extract text
docx2txt file.docx
# extract text and images
docx2txt -i /tmp/img_dir file.docx

补充:python docx提取word中的目录及文本框中的文本

问题描述

python docx提取word中的目录及文本框中的文本

解决方案

因未在docx库找到直接识别word中目录及文本框中文本的方法,所以采用了一个“笨”方法,docx库可以把word文档解析成xml格式,以解析xml的方式查找目录及文本框中文本,具体做法:

迭代出文档的所有element,其中目录的tag为“std”,找到它后提出他的所有文本即为目录文本;文本框的tag 为“textbox”,找到它后还要继续下钻寻找tag为 'r'的element,提取其文本则为文本框中文本。


# 提取word目录
file = docx.Document(file_path)
children = file.element.body.iter()
child_iters = []
for child in children:
# 通过类型判断目录
if child.tag.endswith('main}sdt'):
 for ci in child.iter():
  if ci.text and ci.text.strip():
   child_iters.append(ci)
catalog = [ci.text for ci in child_iters]

# 提取word文本框中文本
file = docx.Document(file_path)
children = file.element.body.iter()
child_iters = []
for child in children:
# 通过类型判断目录
if child.tag.endswith('textbox'):
 for ci in child.iter():
  if ci.tag.endswith('main}r'):
   child_iters.append(ci)
textbox = [ci.text for ci in child_iters]

文本域的标签,第一次找的是AlternateContent,后来发现对有些文本域失效;第二次又找到了pict,基本覆盖了测试的所有文本域;第三次把word文档的标签都找出来看了一下,发现textbox这个标签看着更靠谱,用它测试了一下,也能覆盖所有的测试文本域,决定就选择这个标签。

提取文本后,又有了新需求,提取的文本很多都不成句,呈短语或单词的形式,需要把提取的文本还原成段落形式:


file = docx.Document(file_path)
children = file.element.body.iter()
child_iters = []
tags = []
for child in children:
# 通过类型判断目录
if child.tag.endswith(('AlternateContent','textbox')):
 for ci in child.iter():
  tags.append(ci.tag)
  if ci.tag.endswith(('main}r', 'main}pPr')):
   child_iters.append(ci)
text = ['']
for ci in child_iters :
if ci.tag.endswith('main}pPr'):
 text.append('')
else:
 text[-1] += ci.text
ci.text = ''
trans_text = ['***'+t+'***' for t in text]
print(trans_text)
i, k = 0, 0
for ci in child_iters :
if ci.tag.endswith('main}pPr'):
 i += 1
 k = 0
elif k == 0:
 ci.text = trans_text[i]
 k = 1
file.save('E:/***/test.docx')

把标签pPr当做换行标志, 把提取的文本每段前后都加了“***”后又写回文档中。

注:这里又发现AlternateContent这个标签必须要带上,否则可以提取文本域内的文字,但改变文字写回去保存word不显示更改后的文字。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

来源:https://blog.csdn.net/angus_monroe/article/details/79292837

标签:Python,docx2txt
0
投稿

猜你喜欢

  • python pillow模块使用方法详解

    2021-12-30 14:55:34
  • 用户 jb51net 登录失败。原因: 该帐户的密码必须更改

    2024-01-13 05:58:46
  • Mysql全局ID生成方法

    2023-07-02 13:59:53
  • python模拟点击网页按钮实现方法

    2021-06-18 21:13:09
  • 详解python多线程、锁、event事件机制的简单使用

    2022-03-16 19:48:25
  • 基于Python实现计算纳什均衡的示例详解

    2022-03-03 12:44:27
  • 用Python遍历C盘dll文件的方法

    2023-04-27 20:15:27
  • asp中设置session过期时间方法总结

    2013-06-01 19:52:04
  • Python应用实现处理excel数据过程解析

    2022-10-24 11:52:17
  • pandas is in和not in的使用说明

    2023-04-04 22:06:11
  • Python3加密解密库Crypto的RSA加解密和签名/验签实现方法实例

    2023-05-20 01:36:21
  • python 安装库几种方法之cmd,anaconda,pycharm详解

    2021-05-23 14:19:58
  • numpy数组合并和矩阵拼接的实现

    2022-09-05 19:39:54
  • 使用Python在Windows下获取USB PID&VID的方法

    2021-02-15 19:31:20
  • PHP生成器简单实例

    2024-05-11 09:52:33
  • python对矩阵进行转置的2种处理方法

    2023-12-01 09:18:17
  • 原生JS实现几个常用DOM操作API实例

    2024-04-25 13:09:27
  • keras之权重初始化方式

    2023-06-01 19:04:44
  • python装饰器decorator介绍

    2021-12-18 10:56:25
  • Python中的if判断语句中包含or问题

    2021-12-03 14:30:21
  • asp之家 网络编程 m.aspxhome.com