由于工作中需要从大量docx文档中提出图片,于是到网上搜索,找了一大堆都是分析可扩展标记语言文件并提取的,太过于复杂,实际上有更简单的方法,我们需要用到python-docx这个第三方库,但该库并未开发提取图片功能,不过可以通过其他方法出得到图片数据并保存为图片。
本文为原创,如需转载请注明出处(仅处理docx文档,不能处理文件文档,如果需要可执行文件的,可将代码文件通过pyinstaller进行编译)。
软件界面
下面这段代码是核心:
对于os.listdir(filePath):中的文件
尝试:
#跳过非docx文件
如果。文件:中没有" docx "
继续
# 创建imgPath
subImgPath=imgPath re.sub ' .docx ','',文件)
如果不是,OS。路径。存在(子路径):
操作系统。制作目录(子路径)
doc=docx .文档(文件路径文件)#打开文件
对于文档部分中的_ rels:
rel=doc.part._rels[rel] #获得资源
如果“图像”不在rel.target_ref:中
继续
imgName=re.findall('/().*)',rel.target_ref)[0]
以f:作为打开(子路径'/' imgName,' wb ')
f.write(rel.target_part.blob)
' UI.currentFile.setText('当前文件:' imgName)
除外:
继续
后来经过改进,使用PyQt5制作了界面,下面为源代码:
导入docx、re、os、sys、ui_imgExtract
来自PyQt5 .QtWidgets导入QApplication,QmainWidget,QMessageBox
来自PyQt5 .夸脱导入QFileDialog
def run():
文件路径=UI.filePath.text()
imgPath=UI.imgPath.text()
如果不是,os.path.exists(文件路径):
QMessageBox.about(main,)错误', '请选择docx文件目录!)
返回
如果不是,os.path.exists(imgPath):
os.makedirs(imgPath)
对于os.listdir(filePath):中的文件
尝试:
#跳过非docx文件
如果。文件:中没有docx
继续
# 创建imgPath
subImgPath=imgPath re.sub ' .docx ','',文件)
如果不是,OS。路径。存在(子路径):
操作系统。制作目录(子路径)
doc=d
ocx.Document(filePath + file) #打开文件 for rel in doc.part._rels: rel = doc.part._rels[rel] #获得资源 if "image" not in rel.target_ref: continue imgName = re.findall("/(.*)",rel.target_ref)[0] with open(subImgPath + "/" + imgName,"wb") as f: f.write(rel.target_part.blob) UI.currentFile.setText("当前文件:" + imgName) except: continue QMessageBox.about(main, "完成", "图片提取已完成!") def init(): UI.btnRun.clicked.connect(run) #绑定开始提取按钮 UI.btnFilePath.clicked.connect(choiceFileDir) # 绑定选择docx文件目录 UI.btnImgPath.clicked.connect(choiceImgOutPutDir) #绑定选择图片保存目录 # docx文件默认目录 UI.filePath.setText(os.getcwd()) #默认输出目录 if not os.path.exists(os.getcwd() + "img"): os.makedirs(os.getcwd() + "img") UI.imgPath.setText(os.getcwd() + "img") #选择docx文件目录 def choiceFileDir(): dir = QFileDialog.getExistingDirectory(main, "选择docx文件目录", os.getcwd()) UI.filePath.setText(dir + "/") #选择图片保存目录 def choiceImgOutPutDir(): dir = QFileDialog.getExistingDirectory(main, "选择输出目录", os.getcwd()) UI.imgPath.setText(dir + "/") if __name__ == "__main__": app = QApplication(sys.argv) main = QWidget() UI = ui_imgExtract.Ui_Form() UI.setupUi(main) main.show() init() sys.exit(app.exec_())下面是界面文件ui_imgExtract.py:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'ui_iask.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(604, 100)
self.layoutWidget = QtWidgets.QWidget(Form)
self.layoutWidget.setGeometry(QtCore.QRect(10, 10, 581, 83))
self.layoutWidget.setObjectName("layoutWidget")
self.gridLayout_4 = QtWidgets.QGridLayout(self.layoutWidget)
self.gridLayout_4.setContentsMargins(0, 0, 0, 0)
self.gridLayout_4.setObjectName("gridLayout_4")
self.label_8 = QtWidgets.QLabel(self.layoutWidget)
self.label_8.setObjectName("label_8")
self.gridLayout_4.addWidget(self.label_8, 0, 0, 1, 1)
self.filePath = QtWidgets.QLineEdit(self.layoutWidget)
self.filePath.setObjectName("filePath")
self.gridLayout_4.addWidget(self.filePath, 0, 1, 1, 1)
self.btnFilePath = QtWidgets.QPushButton(self.layoutWidget)
self.btnFilePath.setObjectName("btnFilePath")
self.gridLayout_4.addWidget(self.btnFilePath, 0, 2, 1, 1)
self.label_9 = QtWidgets.QLabel(self.layoutWidget)
self.label_9.setObjectName("label_9")
self.gridLayout_4.addWidget(self.label_9, 1, 0, 1, 1)
self.imgPath = QtWidgets.QLineEdit(self.layoutWidget)
self.imgPath.setObjectName("imgPath")
self.gridLayout_4.addWidget(self.imgPath, 1, 1, 1, 1)
self.btnImgPath = QtWidgets.QPushButton(self.layoutWidget)
self.btnImgPath.setObjectName("btnImgPath")
self.gridLayout_4.addWidget(self.btnImgPath, 1, 2, 1, 1)
self.btnRun = QtWidgets.QPushButton(self.layoutWidget)
self.btnRun.setObjectName("btnRun")
self.gridLayout_4.addWidget(self.btnRun, 2, 2, 1, 1)
self.currentFile = QtWidgets.QLabel(self.layoutWidget)
self.currentFile.setObjectName("currentFile")
self.gridLayout_4.addWidget(self.currentFile, 2, 0, 1, 2)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "docx图片批量提取"))
self.label_8.setText(_translate("Form", "docx文件目录:"))
self.btnFilePath.setText(_translate("Form", "选择"))
self.label_9.setText(_translate("Form", "图片保存目录:"))
self.btnImgPath.setText(_translate("Form", "选择"))
self.btnRun.setText(_translate("Form", "开始提取"))
self.currentFile.setText(_translate("Form", "当前文件:"))