Skip to content

Word to PDF Converter Python Mini Project #377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Word to PDF/README.md
Original file line number Diff line number Diff line change
@@ -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)

---
34 changes: 34 additions & 0 deletions Word to PDF/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from docx2pdf import convert
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()
2 changes: 2 additions & 0 deletions Word to PDF/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
docx2pdf
tkinter