From bf60943f9b7b8e477b26139db9f258d410edd70c Mon Sep 17 00:00:00 2001 From: SRUJAN P R <148844590+SrujanPR@users.noreply.github.com> Date: Sun, 3 Aug 2025 13:39:05 +0530 Subject: [PATCH 1/2] Add files via upload --- Word to PDF/README.md | 44 ++++++++++++++++++++++++++++++++++++ Word to PDF/main.py | 24 ++++++++++++++++++++ Word to PDF/requirements.txt | 2 ++ 3 files changed, 70 insertions(+) create mode 100644 Word to PDF/README.md create mode 100644 Word to PDF/main.py create mode 100644 Word to PDF/requirements.txt diff --git a/Word to PDF/README.md b/Word to PDF/README.md new file mode 100644 index 00000000..cbd66501 --- /dev/null +++ b/Word to PDF/README.md @@ -0,0 +1,44 @@ +# Word to PDF Converter + +A simple yet powerful Python script to convert Microsoft Word `.docx` files into PDF documents with just a few clicks. + +--- + +## 🎯 Key Features + +- **User-Friendly Interface:** Uses a graphical file dialog to easily select your Word file. + +- **Effortless Conversion:** Converts your `.docx` file to a `.pdf` in the same directory, using the same filename. + +- **Cross-Platform:** Built on the robust docx2pdf library, which works seamlessly on Windows, macOS, and Linux. + +--- + +## ⚙️ Setup Instructions + +### 1️⃣ Clone the repository: + ```bash + git clone https://github.com/ndleah/python-mini-project.git + cd Word to PDF + ``` + +### 2️⃣ Install Dependencies + +```bash +pip install -r requirements.txt +``` + +### 3️⃣ Run the Program: + +```bash +python main.py +``` + +--- + + +## 🤖 Author + +[SrujanPR](https://github.com/SrujanPR) + +--- diff --git a/Word to PDF/main.py b/Word to PDF/main.py new file mode 100644 index 00000000..36332a55 --- /dev/null +++ b/Word to PDF/main.py @@ -0,0 +1,24 @@ +from docx2pdf import convert +from tkinter import Tk, filedialog + +def convert_docx_to_pdf(): + root = Tk() + root.withdraw() + docx_file_path = filedialog.askopenfilename( + title="Select a Word file (.docx)", + filetypes=[("Word Documents", "*.docx")] + ) + if not docx_file_path: + print("No file selected. Exiting.") + return + pdf_file_path = docx_file_path.replace(".docx", ".pdf") + + print(f"Converting '{docx_file_path}' to PDF...") + try: + convert(docx_file_path, pdf_file_path) + print(f"Conversion successful! PDF saved to: '{pdf_file_path}'") + except Exception as e: + print(f"An error occurred during conversion: {e}") + +if __name__ == "__main__": + convert_docx_to_pdf() \ No newline at end of file diff --git a/Word to PDF/requirements.txt b/Word to PDF/requirements.txt new file mode 100644 index 00000000..a42b1cec --- /dev/null +++ b/Word to PDF/requirements.txt @@ -0,0 +1,2 @@ +docx2pdf +tkinter \ No newline at end of file From c8fddcbbca0ca5712a275b2a031572e711f1e66f Mon Sep 17 00:00:00 2001 From: SRUJAN P R <148844590+SrujanPR@users.noreply.github.com> Date: Sun, 3 Aug 2025 18:46:27 +0530 Subject: [PATCH 2/2] Update main.py --- Word to PDF/main.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Word to PDF/main.py b/Word to PDF/main.py index 36332a55..9c47e1f3 100644 --- a/Word to PDF/main.py +++ b/Word to PDF/main.py @@ -2,23 +2,33 @@ from tkinter import Tk, filedialog def convert_docx_to_pdf(): + # Initialize and hide the main tkinter window root = Tk() root.withdraw() + + # Open a file dialog to let the user select a .docx file docx_file_path = filedialog.askopenfilename( title="Select a Word file (.docx)", filetypes=[("Word Documents", "*.docx")] ) + + # If no file is selected, print a message and exit if not docx_file_path: print("No file selected. Exiting.") return + + # Create the output file path by replacing the extension pdf_file_path = docx_file_path.replace(".docx", ".pdf") print(f"Converting '{docx_file_path}' to PDF...") try: + # Convert the .docx file to a PDF convert(docx_file_path, pdf_file_path) print(f"Conversion successful! PDF saved to: '{pdf_file_path}'") except Exception as e: + # Handle any errors during the conversion process print(f"An error occurred during conversion: {e}") +# Run the conversion function when the script is executed if __name__ == "__main__": - convert_docx_to_pdf() \ No newline at end of file + convert_docx_to_pdf()