您当前的位置: 首页 >  Python

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

彭世瑜 发布时间:2018-04-13 09:44:27 ,浏览量:2

读取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

更多内容参考 官方文档:http://python-docx.readthedocs.io/en/latest/index.html

关注
打赏
1688896170
查看更多评论

彭世瑜

暂无认证

  • 2浏览

    0关注

    2727博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0471s