SoFunction
Updated on 2024-11-13

Python easygui based on the realization of pdf and word conversion applet

core code

# TODO write pdf converter make exe program
import easygui
import os
from docx2pdf import convert
from pdf2docx import Converter
from pdf2image import convert_from_path
import io
import sys
 
 
buffer = ()
 = buffer
 = buffer
 
desktop_path = (("~"), "Desktop")
 
 
# TODO word to pdf conversion test available
def word_to_pdf():
    word_file = (title="Please upload the word file to be converted to pdf", default="*.docx")
    if word_file:
        pdf_file = word_file.replace(".docx", ".pdf")
        convert(word_file, pdf_file)
        (f"Word The document has been successfully converted to PDF:{pdf_file}", title="Conversion successful.")
    else:
        ("Cancel operation", title="Operation canceled")
 
 
def pdf_to_word():
    pdf_file = (title="Please upload the pdf file to be converted to word format", default="*.pdf")
    # If the file is uploaded, start the conversion operation
    if pdf_file:
        docx_name = ((pdf_file))[0] + ".docx"
        docx_path = (desktop_path, docx_name)
        cv = Converter(pdf_file)
        (docx_path)
        ()
        (f"PDF The document has been successfully converted to Word:{docx_path}", title="Conversion successful.")
    # If not uploaded, the operation is considered canceled
    else:
        ("Cancel operation", title="Operation canceled")
 
 
choices = ["Word to PDF", "PDF to Word"]
choice = ("Please select the action to be performed.", "File Conversion Widget", choices=choices)
 
if choice == "Word to PDF":
    word_to_pdf()
elif choice == "PDF to Word":
    pdf_to_word()
else:
    ("No action selected", title="Operation canceled")

effect

Make an exe program

Install pyinstaller

pip3 install -i /simple/  pyinstaller

Go to the directory where the script is located and cmd

# --noconsole means that the session of cmd will not be displayed after executing the program, i.e., the session pop-up window of cmd will not appear after executing the program.
 
pyinstaller --noconsole -F <pyname>

As long as there are no obvious errors, it's basically good to go

The build and dist directories are generated in the current directory.

Double click to use

FAQ

Traceback (most recent call last): File "resolver.py", line 173, in <module> File "resolver.py", line 148, in word_to_pdf File "docx2pdf\__init__.py", line 106, in convert File "docx2pdf\__init__.py", line 29, in windows File "tqdm\", line 24, in __init__ File "tqdm\", line 1099, in __init__ File "tqdm\", line 1348, in refresh File "tqdm\", line 1496, in display File "tqdm\", line 462, in print_status File "tqdm\", line 455, in fp_write File "tqdm\", line 139, in __getattr__ AttributeError: 'NoneType' object has no attribute 'write'

This error is due to the tqdm library (which displays the progress bar) This library may not be compatible with any other problem, the solution:

Add the following code:

buffer = ()
 = buffer
 = buffer

Above is Python easygui based on the implementation of pdf and word conversion applet details, more about Python pdf and word conversion information please pay attention to my other related articles!