基于Python获取docx/doc文件内容代码解析
作者:python许三多 时间:2022-09-20 09:25:23
这篇文章主要介绍了基于Python获取docx/doc文件内容代码解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
整体思路:
下载文件并修改后缀为zip文件,解压zip文件,所要获取的内容在固定的文件夹下:work/temp/word/document.xml
所用包,全部是python自带,不需要额外下载安装.
# encoding:utf-8
import os
import re
import requests
import zipfile
import xml.dom.minidom
newfile = 'test.docx'
def create(newfile):
"""下载docx文件,并修改后缀为zip"""
res = requests.get('https://www.cqjbfy.gov.cn/publiccenter/splc/mb/splc_gginfo.asp?newsid=28949')
if not os.path.exists(newfile):
f = open(newfile, 'wb')
for chunk in res.iter_content(100000):
f.write(chunk)
f.close()
os.rename(newfile, 'test.zip') 这种方法发现只能解决一部分doc文件,具体原因不得而知,有明白的欢迎留言
# 将doc/docx文件压缩成zip文件
#pf = zipfile.ZipFile('test.zip', 'w', zipfile.ZIP_STORED)
#pf.write(newfile)
def get_txt():
"""解压zip,并在work/temp/word/document.xml获取文本内容,进行正则替换标签等操作"""
f = zipfile.ZipFile('test.zip', 'r')
for file in f.namelist():
f.extract(file, "temp/")
f = xml.dom.minidom.parse('./temp/word/document.xml')
txt = re.sub(r'</w:t></w:r></w:p>', '\n', f.toxml())
print re.sub(r'<.*?>', '', txt)
if __name__ == '__main__':
create(newfile)
get_txt()
来源:https://www.cnblogs.com/fanjp666888/p/9877968.html
标签:Python,获取,docx,doc
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python实现opencv+scoket网络实时图传
2023-07-03 16:09:17
photoshop快捷键大全及使用技巧
2007-10-26 07:40:00
python原始套接字编程示例分享
2021-10-09 19:00:07
![](https://img.aspxhome.com/file/2023/8/72008_0s.jpg)
asp如何正确理解和使用Command、Connection和 Recordset三个对象?
2010-06-28 18:23:00
django模板结构优化的方法
2023-11-12 11:57:02
Python的Django框架中自定义模版标签的示例
2022-10-31 22:28:56
HTML和CSS中的视觉语义
2010-07-09 13:08:00
![](https://img.aspxhome.com/file/UploadPic/20107/9/01-27s.jpg)
Python常用数据分析模块原理解析
2023-07-12 03:46:31
![](https://img.aspxhome.com/file/2023/5/107005_0s.jpg)
PDO::rollBack讲解
2023-07-23 06:52:34
用python做游戏的细节详解
2022-02-08 05:18:39
Python中的Pandas 时间函数 time 、datetime 模块和时间处理基础讲解
2022-08-01 05:18:41
![](https://img.aspxhome.com/file/2023/9/63559_0s.png)
使用javascript提交form表单方法汇总
2023-08-23 09:03:48
AJAX初体验之上手篇
2007-08-23 08:50:00
如何高效使用Python字典的方法详解
2021-07-26 04:29:03
分享整理的12条sql语句连同数据
2012-07-11 16:14:59
Python实现的生成自我描述脚本分享(很有意思的程序)
2023-08-14 20:21:06
Python采集王者最低战力信息实战示例
2021-09-29 17:54:13
![](https://img.aspxhome.com/file/2023/9/72949_0s.png)
如何改良你的CSS代码编写结构
2008-09-29 16:03:00
![](https://img.aspxhome.com/file/UploadPic/up/2008092916133772.gif)
基于DATAFRAME中元素的读取与修改方法
2021-11-18 03:59:09
python list 查询是否存在并且并返回下标的操作
2023-06-20 12:05:43
![](https://img.aspxhome.com/file/2023/5/84105_0s.jpg)