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

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

安装python-docx:

pip install python_docx

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

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

代码如下:

importdocxfrom docx importDocument

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

运行即可输出文本。

我尝试用docx读取.doc文本

代码如下:

importosimportdocxfor filename inos.listdir(os.getcwd()):if filename.endswith('.doc'):print(filename[:-4])

doc= docx.Document(filename[:-4]+".docx")for para indoc.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* andhaving 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 isset to| `Heading {level}`. Raises |ValueError| if *level* isoutside 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``) orcarriage| return (``\r``) characters, each of which isconverted to a line| break.|

| add_picture(self, image_path_or_stream, width=None, height=None)| Return a new picture shape added inits 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 isspecified, the picture appears at its| native size. If only one is specified, it isused to compute| a scaling factor that isthen 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 isspecified, as| isoften 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, anddefaults to| ``WD_SECTION.NEW_PAGE`` if notprovided.|

| 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 读取word 题库_Python-docx 读取word.docx内容相关推荐

  1. python程序设计教程题库_Python程序设计题库-查询

    Python程序设计题库-查询,将学生内部认知过程和外部行为结合起来,即方法.我国基础教育课程改革中的目标分类归纳为.布鲁姆的目标分类理论由组成.教学设计是依据教学理论学习理论和传播理论,运用系统科学 ...

  2. python数据挖掘需要的库_python数据挖掘需要学的内容

    1.Pandas库的操作 Panda是数据分析特别重要的一个库,我们要掌握以下三点: · pandas 分组计算; · pandas 索引与多重索引; 索引比较难,但是却是非常重要的 · pandas ...

  3. python数据序列题库_Python题库系列分享一(17道)

    1.1  到Python官方网站下载并安装Python解释器环境. 1.2  到Anaconda官方网站下载并安装最新的Anaconda3开发环境. 1.3  Python程序的__name__的作用 ...

  4. java word 题目导入_java使用poi导入word题库

    java使用poi导入word题库 包含单选,多选,填空,判断 //word图示 //具体代码,仅适合本人自己的格式 //代码一 InputStream inputStream = file.getI ...

  5. 《Python程序设计》题库(2)

    侵权联系我删除: [写在这里,方便右键百度搜索!] <Python程序设计>题库 填空题 Python安装扩展库常用的是_______工具.(pip) Python标准库math中用来计算 ...

  6. 全国计算机等级考试一级上机WORD题,全国计算机等级考试一级上机WORD题库

    全国计算机等级考试一级上机WORD题库 s 全国计算机等级考试一级上机WORD题库 题.1第请在"考试项目"菜单上选择"字处理软件使用",完成下面的内容:注意: ...

  7. 全国计算机等级考一级word,全国计算机等级考一级上机WORD题库.doc

    全国计算机等级考一级上机WORD题库 全国计算机等级考试一级上机WORD题库1题. 请在"考试项目"菜单上选择"字处理软件使用",完成下面的内容: 注意:下面出 ...

  8. 职教云python题和答案_智慧职教云课堂Python程序设计基础题库及答案

    智慧职教云课堂Python程序设计基础题库及答案 更多相关问题 一个抗体分子上有几个抗原结合的位点 Flash可以用()命令将其它的图片.声音.动画文件导入到Flash文件中. 将场景中的对象转换为元 ...

  9. python建立题库随机抽题,用Python读取Excel题库随机组成Word版本试卷

    Excel题目类型有单选.多选.判断,其中多选题最多包含6个选项,判断题只有两个选项(A:正确,B:错误) 此程序将随机抽取单选题20题,多选题10题,判断题20题,组成一张试卷. 运行程序后,将得到 ...

最新文章

  1. Java:使用synchronized和Lock对象获取对象锁
  2. 解决Bootstrap中代码不高亮问题
  3. 在此iphone上尚未受信任_苹果中国官网开启限购!每款iphone每人限购两部
  4. 2018第九届蓝桥杯C/C++ B国赛 —— 第三题:格雷码
  5. CodeForces - 1523E Crypto Lights(组合数学+推公式)
  6. 基础002_V7-CLB
  7. python编程画布_Python Tkinter 画布(Canvas)
  8. android定义 task,Android Gradle 自定义Task 详解
  9. string.split方法 保留分隔符_白月黑说什么是对象的方法?以及string类型数据对象的常见方法解析...
  10. [转载] Ajax中使用Response.Write输出javascript脚本出错的解决办法
  11. html 屏幕录像,绝对是最简单的屏幕录像方法!无需安装软件
  12. ABBYY FineReader15最新免费下载安装地址激活2020注册机教程
  13. L2UWE: A Framework for the Efficient Enhancement of Low-Light Underwater Images阅读札记
  14. Win7 IE浏览器的降级和升级
  15. 多多情报通:拼多多商品推广被限制是什么情况?
  16. 代码防御性编程的十条技巧
  17. pytoch矩阵乘法torch.bmm
  18. Python框架篇:结构化的网页抓取框架-Scrapy
  19. 2021百度Create大会(暨百度AI开发者大会)
  20. 12 DB操作 Hibernate Panache

热门文章

  1. mysql5.17免安装教程_详细介绍MySql5.7.17免安装配置教程的示例代码
  2. 每日程序C语言40-不带头结点的尾插法创建链表
  3. Django报错:ConnectionAbortedError: [WinError 10053] 你的主机中的软件中止了一个已建立的连接。...
  4. python简单使用
  5. Alpha冲刺Day5
  6. Python--12 内嵌函数和闭包
  7. 深入 AngularUI Router
  8. Java Reflection(五):方法
  9. 两个操作字符串的方法:读取指定位置的字符和找出某个字符串的位置
  10. 将图片转换成16进制的代码