第一次写博客,也不知道要写点儿什么好,所以就把我在学习Python的过程中遇到的问题记录下来,以便之后查看,本人小白,写的不好,如有错误,还请大家批评指正!

中文编码问题总是让人头疼,想要用Python读取word中的内容,用open()经常报错,上网一搜结果发现了Python有专门读取.docx的模块python_docx(只能读取.docx文件,不能读取.doc文件),用起来很方便。

安装python-docx:

pip install python_docx

(注意:不是pip install docx  ! docx也可以安装,但总是报错,缺少exceptions,无法导入)

接下来就可以用Python_docx 来读取word文本了。

代码如下:

import docx
from docx import Document
path = "C:\\Users\\Administrator\\Desktop\\word.docx"
document = Document(path)
for paragraph in document.paragraphs:print(paragraph.text)

运行即可输出文本。

我尝试用docx读取.doc文本

代码如下:

import os
import docx
for filename in os.listdir(os.getcwd()):if filename.endswith('.doc'):print(filename[:-4])doc = docx.Document(filename[:-4]+".docx")for para in doc.paragraphs:print (para.text)

结果报错:docx.opc.exceptions.PackageNotFoundError: Package not found。还是无法识别doc

引用1楼,“改变拓展名并没有改变其编码方式,因此无法读取文本内容,需将doc文件另存为docx文件后再用python-docx读取其内容”

# Document 还有添加标题、分页、段落、图片、章节等方法,说明如下  |  add_heading(self, text='', level=1)|      Return a heading paragraph newly added to the end of the document,|      containing *text* and having its paragraph style determined by|      *level*. If *level* is 0, the style is set to `Title`. If *level* is|      1 (or omitted), `Heading 1` is used. Otherwise the style is set to|      `Heading {level}`. Raises |ValueError| if *level* is outside the|      range 0-9.|  |  add_page_break(self)|      Return a paragraph newly added to the end of the document and|      containing only a page break.|  |  add_paragraph(self, text='', style=None)|      Return a paragraph newly added to the end of the document, populated|      with *text* and having paragraph style *style*. *text* can contain|      tab (``\t``) characters, which are converted to the appropriate XML|      form for a tab. *text* can also include newline (``\n``) or carriage|      return (``\r``) characters, each of which is converted to a line|      break.|  |  add_picture(self, image_path_or_stream, width=None, height=None)|      Return a new picture shape added in its own paragraph at the end of|      the document. The picture contains the image at|      *image_path_or_stream*, scaled based on *width* and *height*. If|      neither width nor height is specified, the picture appears at its|      native size. If only one is specified, it is used to compute|      a scaling factor that is then applied to the unspecified dimension,|      preserving the aspect ratio of the image. The native size of the|      picture is calculated using the dots-per-inch (dpi) value specified|      in the image file, defaulting to 72 dpi if no value is specified, as|      is often the case.|  |  add_section(self, start_type=2)|      Return a |Section| object representing a new section added at the end|      of the document. The optional *start_type* argument must be a member|      of the :ref:`WdSectionStart` enumeration, and defaults to|      ``WD_SECTION.NEW_PAGE`` if not provided.|  |  add_table(self, rows, cols, style=None)|      Add a table having row and column counts of *rows* and *cols*|      respectively and table style of *style*. *style* may be a paragraph|      style object or a paragraph style name. If *style* is |None|, the|      table inherits the default table style of the document.|  |  save(self, path_or_stream)|      Save this document to *path_or_stream*, which can be eit a path to|      a filesystem location (a string) or a file-like object.

docx还有许多其它功能,还正在学习中,详见官方文档:https://python-docx.readthedocs.io/en/latest/user/quickstart.html

转载于:https://www.cnblogs.com/beikew/p/8001164.html

Python-docx 读取word.docx内容相关推荐

  1. python批量提取word指定内容_使用python批量读取word文档并整理关键信息到excel表格的实例...

    目标 最近实验室里成立了一个计算机兴趣小组 倡议大家多把自己解决问题的经验记录并分享 就像在CSDN写博客一样 虽然刚刚起步 但考虑到后面此类经验记录的资料会越来越多 所以一开始就要做好模板设计(如下 ...

  2. python_docx读取word的内容

    python读取word的内容 docx安装 读取word 读取文本内容 paragraphs 读取word中的表格 tables 行遍历 行列循环 列遍历 列行循环 按位置下标获取某个元素 docx ...

  3. Python实现读取Word表格计算汇总写入Excel

    Python实现读取Word表格计算汇总写入Excel 快过年了,又到了公司年底评级的时候了.今年的评级和往常一样,每个人都要填写公司的民主评议表,给各个同事进行评价打分,然后部门根据收集上来的评价表 ...

  4. python读取整个txt文件-python怎么读取txt文件内容

    读取文件: 步骤:打开 -- 读取 -- 关闭>>> f = open('/tmp/test.txt') >>> f.read() 'hello python! h ...

  5. python怎么读取txt文件-python怎么读取txt文件内容

    读取文件: 步骤:打开 -- 读取 -- 关闭>>> f = open('/tmp/test.txt') >>> f.read() 'hello python! h ...

  6. python 读取word 题库_Python-docx 读取word.docx内容

    第一次写博客,也不知道要写点儿什么好,所以就把我在学习Python的过程中遇到的问题记录下来,以便之后查看,本人小白,写的不好,如有错误,还请大家批评指正! 中文编码问题总是让人头疼,想要用Pytho ...

  7. python如何读取word中超链接的文本_如何使用python从docx文件中提取超链接中的url...

    我是Python的初学者,有一项任务是使用Python更改.docx文档中的每个超链接.感谢Kiran的代码,它给了我一些提示,让我做一些猜测,尝试和错误,最后让它工作.这里是我拥有的代码,我想与其他 ...

  8. python读取word指定内容_python读取word 中指定位置的表格及表格数据

    1.Word文档如下: 2.代码 # -*- coding: UTF-8 -*- from docx import Document def readSpecTable(filename, specT ...

  9. python生成word目录_使用Python更新MS Word .docx文档的目录(目录)

    我使用python包" python-docx"来修改MS Word .docx文档的结构和内容.该软件包无法更新TOC(目录)[Python: Create a "Ta ...

  10. python怎么读取word文件_使用python编辑和读取word文档

    python调用word接口主要用到的模板为python-docx,基本操作官方文档有说明. 使用python新建一个word文档,操作就像文档里介绍的那样: 1 from docx importDo ...

最新文章

  1. CCIE-LAB-第五篇-DHCP+DHCP-Realy+DHCP snooping
  2. atlas mysql 安装_atlas中间件安装配置
  3. python安装 + pycharm安装和入门学习 +安装配置TensorFlow
  4. 批处理获取网卡名称(附修改IP、网关、DNS脚本)
  5. ae2020英文改中文_AE2020 菜单中英文翻译对照表(小白必备AE菜单字典)
  6. mysql 随机取数组_sql语句实现随机取n条数据(转)
  7. 软件测试人员每天的工作日常
  8. MSP430F149最小系统原理图与芯片封装分享
  9. android最强管理软件root,SuperSU Pro v2.82中文专业版-超强安卓手机root工具
  10. 【NLP】千呼万唤始出来——GPT-3终于开源!
  11. Windows下SVN修改已经提交的日志
  12. 实变函数(4)--Lebesgue积分
  13. awk 的内置变量 NF、NR、FNR、FS、OFS、RS、ORS
  14. springboot+mybatis+新加属性自动加数据库字段
  15. 心跳信号分类---(中)
  16. java jdk安装失败_图文解答Java JDK9.0安装失败的原因,附带处理方法
  17. mysql取数据库时间函数_MySQL数据库—日期与时间函数
  18. AWS EC2 云服务器 Red Hat Enterprise Linux Server release 7.4 (Maipo) vnc远程连接教程
  19. 沪深300股票聚类可视化案例||tushare完整可运行代码逐行解释
  20. 解决:为什么谷歌浏览器启动主页是360导航,自己明明设置的是百度?

热门文章

  1. php pacs,PACS系统
  2. oracle手动锁表和解锁_Oracle锁表查询和解锁方法
  3. TypeError: unhashable type: ‘numpy.ndarray‘
  4. 【转】Elasticsearch+Django搜索引擎(二)
  5. 深度学习之Ubuntu下安装caffe和TensorFlow的cpu版本
  6. python词云可视化方法总结记录【简单词云+背景图片词云+自定义字体颜色词云】
  7. 反向传播算法(backpropagation)
  8. picpick尺子像素大小精度不够准确_袖珍大小MFJ-223 1-60MHz彩色图形VNA矢量网络分析仪...
  9. 解决button多次重复点击
  10. oracle执行大sql,mybatis连接oracle执行sql语句出现ORA