第一次写博客,也不知道要写点儿什么好,所以就把我在学习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

Python-docx 读取word.docx内容的更多相关文章

  1. python读取word表格内容(1)

    1.首页介绍下word表格内容,实例如下: 每两个表格后面是一个合并的单元格

  2. poi读取word的内容

    pache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 1.读取word 2003及word 2007需要的 ...

  3. Python configparser 读取指定节点内容失败

    # !/user/bin/python # -*- coding: utf-8 -*- import configparser # 生成一个config文件 config = configparser ...

  4. java 实现poi方式读取word文件内容

    1.下载poi的jar包 下载地址:https://www.apache.org/dyn/closer.lua/poi/release/bin/poi-bin-3.17-20170915.tar.gz ...

  5. aspose.word 读取word段落内容

    注:转载请标明文章原始出处及作者信息 aspose.word 插件下载 链接: http://pan.baidu.com/s/1qXIgOXY 密码: wsj2 使用原因:无需安装office,无兼容 ...

  6. Python中读取csv文件内容方法

    gg 224@126.com 85 男 dd 123@126.com 52 女 fgf 125@126.com 23 女 csv文件内容如上图,首先导入csv包,调用csv中的方法reader()创建 ...

  7. python读取word中的段落、表、图+++++++++++Doc转换Docx

    读取文本.图.表.解压信息 import docx import zipfile import os import shutil '''读取word中的文本''' def gettxt(): file ...

  8. Python 读取word中表格数据、读取word修改并保存、替换word中词汇、读取word中每段内容,读取一段话中相同样式内容,理解Document中run

    from docx import Document path = r'D:\pywork\12' # word信息表所在文件夹 w = Document(path + '/' + 'word信息表.d ...

  9. 使用poi读取word2007(.docx)中的复杂表格

    使用poi读取word2007(.docx)中的复杂表格 最近工作需要做一个读取word(.docx)中的表格,并以html形式输出.经过上网查询,使用了poi. 对于2007及之后的word文档,需 ...

随机推荐

  1. Android开发 集成极光推送中的问题

    AndroidManifest.xml清单文件报错: cn.jpush.android.service.DataProvider@exported value=(true)报错,解决如下: 根据报错行 ...

  2. Exp6 信息搜集与漏洞扫描 20164313 杜桂鑫

    1.实践目标 掌握信息搜集的最基础技能与常用工具的使用方法. 2.实践内容 (1)各种搜索技巧的应用 1.使用搜索引擎 在百度搜索栏内输入 site:com filetype:doc 北京电子科技学院 ...

  3. mysql 关联

    自关联 设计省信息的表结构provinces id ptitle 设计市信息的表结构cityscitys表的proid表示城市所属的省,对应着provinces表的id值 id ctitle proi ...

  4. Spark启动报错|java.io.FileNotFoundException: File does not exist: hdfs://hadoop101:9000/directory

    at org.apache.spark.deploy.history.FsHistoryProvider.<init>(FsHistoryProvider.scala:) at org.a ...

  5. Mac 端配置 Lua 环境

    一.设计目的 Lua 是一种轻量级的脚本语言,其目的是为了嵌入到程序中,从而为程序提供灵活的扩展和定制功能. 二.特性 轻量级:编译后仅仅 100 余K,可以很方便的嵌入到程序中. 可扩展:Lua 提 ...

  6. Flask对数据库的操作-----

    首先得做好做基本的框架 # -*- encoding: utf-8 -*- from flask import Flask,render_template #导入第三方连接库sql点金术 from f ...

  7. python远程连接windows

    远程连接windows系统     https://blog.51cto.com/ckl893/2145809 import winrm win2008 = winrm.Session('http:/ ...

  8. FPGA——流水灯(一)

    对于FPGA的结构原理,先不进行全面的了解,先能根据教程程序看得懂,写得出来跑起来.慢慢的了解程序运行的原理,各种语法的使用. 今天对流水的程序有一个认识,熟悉软件的使用,语法规则,原理.以正点原子的 ...

  9. 初次接触Linux

    最近由于工作需求,需要接触Linux系统. 使用VMware虚拟机,安装ubuntu系统.网上教程很多. 配置opencv环境.这是我参考的网上帖子https://blog.csdn.net/fish ...

  10. Matplotlib-动画

    Animation 动画 定义方程 参数设置 # Animation 动画 # 定义方程 # 使用matplotlib做动画也是可以的,我们使用其中一种方式,function animation来说说 ...