P23 - 读取Word内容 - 图1 P23 - 读取Word内容 - 图2 P23 - 读取Word内容 - 图3 P23 - 读取Word内容 - 图4

你好,我是UP:法学院毕业的Python程序员兆锋(点击查看🏆近期的付费答疑记录)
如有学习问题,欢迎加入学习群和我交流❤交流群

下面是本讲课程的视频、代码和配套文档:

主要内容

视频内容

点击下方链接,直达本讲视频

本讲代码

pure

  1. # -*- coding: utf-8 -*-
  2. # @Time : 2020/8/20 19:10
  3. # @公众号 :Python自动化办公社区
  4. # @File : pure.py
  5. # @Software: PyCharm
  6. # @Description:
  7. from docx import Document
  8. document = Document('pure.docx')
  9. all_paragraphs = document.paragraphs
  10. for p in all_paragraphs:
  11. print(p.text)

word_table

  1. # -*- coding: utf-8 -*-
  2. # @Time : 2020/8/20 19:10
  3. # @公众号 :Python自动化办公社区
  4. # @File : word_table.py
  5. # @Software: PyCharm
  6. # @Description:
  7. import zipfile
  8. word_book = zipfile.ZipFile('word_table.docx')
  9. xml = word_book.read("word/document.xml").decode('utf-8')
  10. # print(xml)
  11. xml_list = xml.split('<w:t>')
  12. print(xml_list)
  13. text_list = []
  14. for i in xml_list:
  15. if i.find('</w:t>') + 1:
  16. text_list.append(i[:i.find('</w:t>')])
  17. else:
  18. pass
  19. text = "".join(text_list)
  20. print(text)

配套文档

pure.docxword_table.docx

拓展内容

提问与答疑

提供以下3种交流方式:

  • 有任何问题,请加入Python学习群,和我交流~❤交流群
  • 也期待加我好友,我们深入沟通,我的微信:hdylw1024,请务必注明来意,否则很难通过。
  • 或者你也可以直接在本文档的留言板留言,和大家一起讨论~