文章目录

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


一、需求分析

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

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

二、系统设计

2.1系统业务流程

python 调整word内图片大小 python docx 图片 操作_tkinter


图1 系统业务流程

2.2系统预览

python 调整word内图片大小 python docx 图片 操作_pycharm_02


图2 系统主界面

python 调整word内图片大小 python docx 图片 操作_python 调整word内图片大小_03


图3 图片提取成功

python 调整word内图片大小 python docx 图片 操作_python 调整word内图片大小_04


图4 提示用户文件名不正确

三、系统开发必备

3.1 系统开发环境

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

  • 操作系统:Windows10
  • Python:Python3.8
  • 开发工具:Pycharm
  • Python内置模块:os、re、tkinter
  • Python第三方模块:docx

3.2文件组织结构

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

python 调整word内图片大小 python docx 图片 操作_python_05


图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._rels
    for 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 win32api
    import win32con
    docxPath = 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模块支持多种操作系统。但是该模块不支持跨平台操作。

  1. 通过CMD打开“命令提示符”窗口,在光标位置输入:pyinstaller+F+(要打包的.py文件的绝对路径)

python 调整word内图片大小 python docx 图片 操作_python_06


图6 生成.exe可执行文件的执行过程

  1. 按下“Enter"键,将自动生成.exe可执行文件
  2. 在.exe文件路径下打开找到该文件,双击该文件即可运行项目。

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