Python通过docx模块读写微软docx文件

简介: Python通过docx模块读写微软docx文件

读取docx文件

方式一:

读取流程: 二进制对象 - 》 解压 ——》读取xml文件

# -*- encoding: utf-8 -*-
from zipfile import ZipFile
from urllib import urlopen
from io import BytesIO
from bs4 import BeautifulSoup
# url="http://www.pythonscraping.com/pages/AwordDocument.doxc"
# word_file = urlopen(url).read()
# word_file = BytesIO(worl_file)
word_file = open("AWordDocument.docx", "rb")
document = ZipFile(word_file)
xml_content = document.read("word/document.xml")
text = xml_content.decode("utf-8")
print text

方式二

解析出文本内容

pip install python-docx
import docx
doc = docx.Document("AWordDocument.docx")
print doc
# 打印所有段落内容
for p in  doc.paragraphs:
    print p.text
相关文章
|
3天前
|
JSON 安全 数据格式
Python文件操作宝典:一步步教你玩转文件读写
Python文件操作宝典:一步步教你玩转文件读写
|
3天前
|
Python
python--os模块
python--os模块
10 2
|
3天前
|
Python
python搭建文件服务
python搭建文件服务
7 1
|
3天前
|
Python
python-time模块
python-time模块
7 1
|
1天前
|
机器学习/深度学习 人工智能 程序员
探索Python宝库:从基础到技能的干货知识(数据类型与变量+ 条件与循环+函数与模块+文件+异常+OOP)
探索Python宝库:从基础到技能的干货知识(数据类型与变量+ 条件与循环+函数与模块+文件+异常+OOP)
2 0
|
3天前
|
数据安全/隐私保护 Python
经验大分享:python读取yaml文件
经验大分享:python读取yaml文件
10 0
|
3天前
|
移动开发 Unix Linux
Python 常用模块全面分享
Python 常用模块全面分享
|
4天前
|
存储 Python
Python处理文件的常用代码
Python处理文件的常用代码
|
4天前
|
Python
python文件的读取与写入
python文件的读取与写入
11 0
|
15天前
|
存储 Python 内存技术
python WAV音频文件处理—— (1)读写WAV文件
python WAV音频文件处理—— (1)读写WAV文件
43 14