300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 【Python项目实战】提取.docx文件中的图片并保存到指定的文件夹

【Python项目实战】提取.docx文件中的图片并保存到指定的文件夹

时间:2021-04-20 05:15:17

相关推荐

【Python项目实战】提取.docx文件中的图片并保存到指定的文件夹

文章目录

一、需求分析二、系统设计2.1系统业务流程2.2系统预览三、系统开发必备3.1 系统开发环境3.2文件组织结构四、主函数设计1.创建窗口2.创建按钮3.创建输入框五、函数设计1.提取.docx文件中的图片(最核心)2.选择文件3.文件名判断4.退出六、打包成exe可执行文件

一、需求分析

为了满足用户快速从docx文件中提取图片到指定文件夹的需求,该系统应该满足以下功能:

当用户选择docx文件并指定图片输出路径时可以完成对docx文件中所有图片的提取。当用户选择的文件不是docx文件时应该提醒用户重新选择文件当用户没有选择图片输出路径时应该提醒用户输入当用户选择退出可以退出程序

二、系统设计

2.1系统业务流程

图1 系统业务流程

2.2系统预览

图2 系统主界面图3 图片提取成功图4 提示用户文件名不正确

三、系统开发必备

3.1 系统开发环境

本系统的软件开发与运行环境具体如下:

操作系统:Windows10Python:Python3.8开发工具:PycharmPython内置模块:os、re、tkinterPython第三方模块:docx

3.2文件组织结构

提取.docx中图片的文件结构结构比较简单,只包含一个Python文件。在运行程序时,用户可以自行选择要进行提取的.docx文件和输出图片的文件夹路径。

图5 文件夹组织结构

四、主函数设计

1.创建窗口

window = tk.Tk()window.title("打工都是人上人!")window.geometry("400x170")window.resizable(0,0)#设置窗口不能伸缩变化

2.创建按钮

#选择docx文件按钮docx_select_button = tk.Button(window, text="选择docx文件", background="green", foreground="white", command=selectPath)docx_select_button.place(x=280, y=20, height=30, width=100)#选择图片路径文件按钮pic_select_button = tk.Button(window, text="选择文件夹", background="green", foreground="white", command=selectDirectory)pic_select_button.place(x=280, y=70, height=30, width=100)#提取按钮extract_button = tk.Button(window, text="提取", background="green", foreground="white", command=lambda: printPath(docx_path_input, pic_path_input))extract_button.place(x=80, y=120, height=30, width=80)#退出按钮exit_button = tk.Button(window, text="退出", background="green", foreground="white", command=lambda: exit(window))exit_button.place(x=240, y=120, height=30, width=80)

3.创建输入框

#创建docx文件名输入框docx_path_input = tk.Entry(window, textvariable=docx_path, highlightcolor='red', highlightthickness=1)docx_path_input.place(x=10, y=20, height=30, width=250)#创建输出图片路径输入框pic_path_input = tk.Entry(window, textvariable=pic_path, highlightcolor='red', highlightthickness=1)pic_path_input.place(x=10, y=70, height=30, width=250)

五、函数设计

1.提取.docx文件中的图片(最核心)

def get_pictures(word_path, result_path):"""图片提取:param word_path: word路径:param result_path: 结果路径:return:"""doc = docx.Document(word_path)dict_rel = doc.part._relsfor rel in dict_rel:rel = dict_rel[rel]if "image" in rel.target_ref:if not os.path.exists(result_path):os.makedirs(result_path)img_name = re.findall("/(.*)", rel.target_ref)[0]word_name = os.path.splitext(word_path)[0]if os.sep in word_name:new_name = word_name.split('\\')[-1]else:new_name = word_name.split('/')[-1]img_name = f'{new_name}_{img_name}'with open(f'{result_path}/{img_name}', "wb") as f:f.write(rel.target_part.blob)

2.选择文件

def selectPath():path1 = askopenfilename()docx_path.set(path1)def selectDirectory():path0 = askdirectory()pic_path.set(path0)

3.文件名判断

def printPath(e1, e2):import win32apiimport win32condocxPath = e1.get()picPath = e2.get()if docxPath[-5:] == ".docx" and picPath != "":get_pictures(docxPath, picPath)win32api.MessageBox(0, "提取成功!", "提醒", win32con.MB_OK)elif docxPath[-5:] != ".docx":win32api.MessageBox(0, "请选择文件后缀名为.docx的文件!", "提醒", win32con.MB_RETRYCANCEL)elif picPath == "":win32api.MessageBox(0, "输出文件夹不能为空!", "提醒", win32con.MB_RETRYCANC

4.退出

def exit(e1):e1.destroy()

六、打包成exe可执行文件

Python项目编写完成后,可以将其打包成一个exe可执行文件,这样就可以在其他没有安装Python环境的计算机上运行。

实现打包.exe文件可执行文件需要使用pyinstaller模块,这个模块为第三方模块,需要单独安装。PyInstaller模块支持多种操作系统。但是该模块不支持跨平台操作。

通过CMD打开“命令提示符”窗口,在光标位置输入:pyinstaller+F+(要打包的.py文件的绝对路径)图6 生成.exe可执行文件的执行过程按下“Enter"键,将自动生成.exe可执行文件在.exe文件路径下打开找到该文件,双击该文件即可运行项目。

喜欢别忘记点赞加关注呀!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。