【Python项目实战】提取.docx文件中的图片并保存到指定的文件夹
生活随笔
收集整理的這篇文章主要介紹了
【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 系統開發環境
本系統的軟件開發與運行環境具體如下:
- 操作系統:Windows10
- Python:Python3.8
- 開發工具:Pycharm
- Python內置模塊:os、re、tkinter
- Python第三方模塊: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_RETRYCANC4.退出
def exit(e1):e1.destroy()六、打包成exe可執行文件
Python項目編寫完成后,可以將其打包成一個exe可執行文件,這樣就可以在其他沒有安裝Python環境的計算機上運行。
實現打包.exe文件可執行文件需要使用pyinstaller模塊,這個模塊為第三方模塊,需要單獨安裝。PyInstaller模塊支持多種操作系統。但是該模塊不支持跨平臺操作。
喜歡別忘記點贊加關注呀!
總結
以上是生活随笔為你收集整理的【Python项目实战】提取.docx文件中的图片并保存到指定的文件夹的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【电磁场实验作业】有限差分法(FDM)求
- 下一篇: 用latex排版电磁场课程报告