读取文本、图、表、解压信息
import docx
import zipfile
import os
import shutil '''读取word中的文本'''
def gettxt():
file=docx.Document("gao.docx")
print("段落数:"+str(len(file.paragraphs)))#段落数为13,每个回车隔离一段 #输出每一段的内容
# for para in file.paragraphs:
# print(para.text) #输出段落编号及段落内容
for i in range(len(file.paragraphs)):
if len(file.paragraphs[i].text.replace(' ',''))>4:
print("第"+str(i)+"段的内容是:"+file.paragraphs[i].text) '''读取word中的table'''
def gettable():
doc = docx.Document('word.docx')
for table in doc.tables: # 遍历所有表格
print ('----table------')
for row in table.rows: # 遍历表格的所有行
# row_str = '\t'.join([cell.text for cell in row.cells]) # 一行数据
# print row_str
for cell in row.cells:
print (cell.text, '\t') '''获取解压后的文件信息'''
def getinfo(wordfile):
f=zipfile.ZipFile(wordfile,'r')
for filename in f.namelist():
f.extract(filename)
print(filename) '''
输出解压后的信息:
_rels/
_rels/.rels
customXml/
customXml/_rels/
customXml/_rels/item1.xml.rels
customXml/_rels/item2.xml.rels
customXml/item1.xml
customXml/item2.xml
customXml/itemProps1.xml
customXml/itemProps2.xml
docProps/
docProps/app.xml
docProps/core.xml
docProps/custom.xml
docProps/thumbnail.wmf
word/
word/_rels/
word/_rels/document.xml.rels
word/document.xml
word/fontTable.xml
word/media/
word/media/image1.jpeg
word/numbering.xml
word/settings.xml
word/styles.xml
word/theme/
word/theme/theme1.xml
''' '''
------获取图:

word文档的路径
zip压缩文件的路径
临时解压的tmp路径
最后需要保存的store_path路径
'''
def getpic(path, zip_path, tmp_path, store_path):
'''
:param path:源文件
:param zip_path:docx重命名为zip
:param tmp_path:中转图片文件夹
:param store_path:最后保存结果的文件夹(需要手动创建)
:return:
'''
'''=============将docx文件重命名为zip文件===================='''
os.rename(path, zip_path)
# 进行解压
f = zipfile.ZipFile(zip_path, 'r')
# 将图片提取并保存
for file in f.namelist():
f.extract(file, tmp_path)
# 释放该zip文件
f.close()
'''=============将docx文件从zip还原为docx===================='''
os.rename(zip_path, path)
# 得到缓存文件夹中图片列表
pic = os.listdir(os.path.join(tmp_path, 'word/media'))
'''=============将图片复制到最终的文件夹中===================='''
for i in pic:
# 根据word的路径生成图片的名称
new_name = path.replace('\\', '_')
new_name = new_name.replace(':', '') + '_' + i
shutil.copy(os.path.join(tmp_path + '/word/media', i), os.path.join(store_path, new_name))
'''=============删除缓冲文件夹中的文件,用以存储下一次的文件===================='''
for i in os.listdir(tmp_path):
# 如果是文件夹则删除
if os.path.isdir(os.path.join(tmp_path, i)):
shutil.rmtree(os.path.join(tmp_path, i)) if __name__ == '__main__':
# 源文件
path = r'E:\dogcat\提取图片\log.docx'
# docx重命名为zip
zip_path = r'E:\dogcat\提取图片\log.zip'
# 中转图片文件夹
tmp_path = r'E:\dogcat\提取图片\tmp'
# 最后保存结果的文件夹
store_path = r'E:\dogcat\提取图片\测试'
m = getpic(path, zip_path, tmp_path, store_path)

至于处理doc文件直接转存成docx文件就可以了

def docTTTTTdocx(doc_name, docx_name):    
  try:
# 首先将doc转换成docx
word = client.Dispatch("Word.Application")
doc = word.Documents.Open(doc_name)
# 使用参数16表示将doc转换成docx
doc.SaveAs(docx_name, 16)
doc.Close()
word.Quit()
except:
pass 这里如果转换不成功,可能是路径的问题,把doc_name换成完整路径,如下:
from win32com.client import Dispatch
def docToDocxR(docPath, docxPath):
'''将doc转存为docx'''
word = Dispatch('Word.Application')
pathPrefix = sys.path[0]+'\\'
print(pathPrefix)
doc = word.Documents.Open(pathPrefix+docPath)
doc.SaveAs(pathPrefix+docxPath, FileFormat=12)
doc.Close()
word.Quit()
 

参考:

https://blog.csdn.net/qq_40925239/article/details/83279957

https://blog.csdn.net/qq_15969343/article/details/81673970

python读取word中的段落、表、图+++++++++++Doc转换Docx的更多相关文章

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

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

  2. Python读取word文档内容

    1,利用python读取纯文字的word文档,读取段落和段落里的文字. 先读取段落,代码如下: 1 ''' 2 #利用python读取word文档,先读取段落 3 ''' 4 #导入所需库 5 fro ...

  3. 使用python读取word,写入execl

    word里面有2张表,需要找到第二张表,并写入execl中: 代码如下: #coding:utf-8 import os from docx import Document import win32c ...

  4. Java读取word中表格

    因为要新建一个站,公司要把word表格的部分行列存到数据库中.之前用java操作过excel,本来打算用java从word表格中读取数据,再存到数据库中,结果因为权限不够,无法访问公司要写的那个数据库 ...

  5. Java 读取Word中的脚注、尾注

    本文介绍读取Word中的脚注及尾注的方法,添加脚注.尾注可以参考这篇文章. 注:本文使用了Word类库(Free Spire.Doc for Java 免费版)来读取,获取该类库可通过官网下载,并解压 ...

  6. VBA读取word中的内容到Excel中

    原文:VBA读取word中的内容到Excel中 Public Sub Duqu()      Dim myFile As String     Dim docApp As Word.Applicati ...

  7. Python读取excel中的图片

    作为Java程序员,Java自然是最主要的编程语言.但是Java适合完成大型项目,对于平时工作中小的工作任务,需要快速完成,易于修改和调试,使用Java显得很繁琐,需要进行类的设计,打成jar包,出现 ...

  8. python读取excel中单元格的内容返回的5种类型

    (1) 读取单个sheetname的内容. 此部分转自:https://www.cnblogs.com/xxiong1031/p/7069006.html python读取excel中单元格的内容返回 ...

  9. Python 读取文件中unicode编码转成中文显示问题

    Python读取文件中的字符串已经是unicode编码,如:\u53eb\u6211,需要转换成中文时有两种方式 1.使用eval: eval("u"+"\'" ...

随机推荐

  1. Flask基于websocket的简单聊天室

    1.安装gevent-websocket pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ gevent-websocket 2.cha ...

  2. XSS相关Payload及Bypass的备忘录(上)

    翻译学习准备自用,同时分享给大家, 来自于: https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/XSS%20Injecti ...

  3. 分析mybatis中 #{} 和${}的区别

    分析方法: 在 GenericTokenParser这个类的parse方法的这一行下个断点调试一下就明白了 builder.append(handler.handleToken(content)); ...

  4. CTF-代码审计(1)——parse_str()变量覆盖

    题目连接:http://222.18.158.226:7000/iscc.php 考点:parse_str()变量覆盖 代码: PHP知识点: 1.parse_url() 参照网址:https://w ...

  5. There is already an open DataReader associated with this Command which must be closed first

    通常出现在嵌套查询数据库(比如在一个qry的遍历时,又进行了数据库查询) 通过在连接字符串中允许MARS可以轻松解决这个问题. 将MultipleActiveResultSets = true添加到连 ...

  6. terminator

    terminator 能够实现linux 终端的分屏显示. 安装 sudo add-apt-repository ppa:gnome-terminator sudoapt-get update sud ...

  7. org.json package

    JSON is a light-weight,language independent,data interchange format. org.json package implement JSON ...

  8. 国内加速访问 GitHub

    国内加速访问 GitHub 本文原始地址:https://sitoi.cn/posts/23395.html 中国访问 GitHub 的速度不忍直视,那就叫一个慢! Q: 为什么访问速度会很慢? A: ...

  9. Linux操作系统安全-证书的申请原理

    Linux操作系统安全-证书的申请原理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.APR的中间人攻击 如下图所示,如果在client和server端有一个中间人攻击就比较麻 ...

  10. SpringCloud2.0 Hystrix Feign 基于Feign实现断路器

    原文:https://www.cnblogs.com/songlu/p/9968953.html 1.启动[服务中心]集群,工程名:springcloud-eureka-server 参考 Sprin ...