[Python project practice] extract the pictures in the. docx file and save them to the specified folder

Keywords: Python Pycharm Tkinter

1, Demand analysis

In order to meet the needs of users to quickly extract pictures from docx files to specified folders, the system should meet the following functions:

  • When the user selects the docx file and specifies the image output path, all images in the docx file can be extracted.
  • When the file selected by the user is not a docx file, the user should be reminded to re select the file
  • When the user does not select the image output path, the user should be reminded to input
  • When the user chooses to exit, he can exit the program

2, System design

2.1 system business process

Figure 1 system business process

2.2 System Preview

Figure 2 system main interface

Figure 3 successful image extraction

Figure 4 prompts the user that the file name is incorrect

3, Necessary for system development

3.1 system development environment

The software development and operation environment of the system is as follows:

  • Operating system: Windows10
  • Python: Python3.8
  • Development tool: pychar
  • Python built-in modules: os, re, tkinter
  • Python third party module: docx

3.2 document organization structure

The file structure of extracting pictures in. docx is relatively simple, and only contains a Python file. When running the program, users can choose the folder path of. docx files and output pictures to be extracted.

Fig. 5 folder organization

4, Principal function design

1. Create a window

window = tk.Tk()
window.title("Working is a man!")
window.resizable(0,0)#The settings window cannot be scaled

2. Create button

#Select the docx file button
docx_select_button = tk.Button(window, text="choice docx file", background="green", foreground="white", command=selectPath)
docx_select_button.place(x=280, y=20, height=30, width=100)

#Select picture path file button
pic_select_button = tk.Button(window, text="Select Folder", background="green", foreground=
"white", command=selectDirectory)
pic_select_button.place(x=280, y=70, height=30, width=100)

#Extract button
extract_button = tk.Button(window, text="extract", 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
exit_button = tk.Button(window, text="sign out", background="green", foreground=
"white", command=lambda: exit(window))
exit_button.place(x=240, y=120, height=30, width=80)

3. Create an input box

#Create docx file name input box
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)

#Create output picture path input box
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)

5, Function design

1. Extract the pictures in the. docx file (the core)

def get_pictures(word_path, result_path):
    Image extraction
    :param word_path: word route
    :param result_path: Result path
    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):
            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]
                new_name = word_name.split('/')[-1]
            img_name = f'{new_name}_{img_name}'
            with open(f'{result_path}/{img_name}', "wb") as f:

2. Select file

def selectPath():
    path1 = askopenfilename()

def selectDirectory():
    path0 = askdirectory()

3. File name judgment

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, "Extraction succeeded!", "remind", win32con.MB_OK)
    elif docxPath[-5:] != ".docx":
        win32api.MessageBox(0, "Please select file suffix.docx File!", "remind", win32con.MB_RETRYCANCEL)
    elif picPath == "":
        win32api.MessageBox(0, "Output folder cannot be empty!", "remind", win32con.MB_RETRYCANC

4. Exit

def exit(e1):

6, Package into exe executable

After the python project is written, it can be packaged into an exe executable file so that it can run on other computers without Python environment installed.

To package the. exe file executable file, you need to use the pyinstaller module, which is a third-party module and needs to be installed separately. The pyinstaller module supports a variety of operating systems. However, the module does not support cross platform operation.

  1. Open the "command prompt" window through CMD and enter: pyinstaller+F + (absolute path of. py file to be packaged) at the cursor position

Figure 6 execution process of generating. exe executable file
  1. Pressing the "Enter" key will automatically generate the. exe executable file
  2. Open the file under the. exe file path, find the file, and double-click the file to run the project.

Like, don't forget to praise and pay attention!

Posted by cody44 on Thu, 23 Sep 2021 00:44:21 -0700