Skip to content

Commit 4307dc2

Browse files
committed
Created MD to PDF/TXT tool w GUI
1 parent cd5e61a commit 4307dc2

File tree

3 files changed

+181
-0
lines changed

3 files changed

+181
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Markdown Converter (PySide6)
2+
3+
A simple Python GUI tool built with **PySide6** that converts Markdown (`.md`) files into **PDF** or **Text** files.
4+
It provides a clean interface to select the input file, choose the output format, and save it wherever you want.
5+
6+
---
7+
8+
## Features
9+
- 🖱️ **User-friendly GUI** built with PySide6
10+
- 📂 **File selection dialogs** for choosing input and output files
11+
- 📝 **Markdown → Text** conversion
12+
- 📄 **Markdown → PDF** conversion with **Unicode** support (Chinese, Japanese, Korean, emoji, etc.)
13+
- ❌ Error handling with pop-up dialogs
14+
- ⚡ Option to run **with or without Pandoc**
15+
16+
---
17+
18+
## Requirements
19+
20+
Make sure you have **Python 3.8+** installed.
21+
22+
Install the required Python packages:
23+
```bash
24+
pip install PySide6 reportlab pypandoc
25+
```
26+
27+
---
28+
29+
## Installing Pandoc (Required for Default Conversion)
30+
31+
This tool uses **pypandoc**, which depends on the Pandoc binary.
32+
Pandoc must be installed separately on your system.
33+
34+
### 1. Official Installation Instructions
35+
- Pandoc Official Installation Guide: [https://pandoc.org/installing.html](https://pandoc.org/installing.html)
36+
37+
### 2. Windows
38+
- Download the **Windows Installer (.msi)** from the [Pandoc Releases Page](https://github.com/jgm/pandoc/releases)
39+
- Run the installer and let it add Pandoc to your PATH automatically.
40+
41+
### 3. macOS
42+
Using **Homebrew** (recommended):
43+
```bash
44+
brew install pandoc
45+
```
46+
Or download the **macOS package** from the [Pandoc Releases Page](https://github.com/jgm/pandoc/releases)
47+
48+
### 4. Linux (Debian/Ubuntu)
49+
```bash
50+
sudo apt update
51+
sudo apt install pandoc
52+
```
53+
For other Linux distros, check the [Pandoc Install Guide](https://pandoc.org/installing.html) for commands.
54+
55+
---
56+
57+
## Verify Pandoc Installation
58+
After installation, open a terminal or command prompt and run:
59+
```bash
60+
pandoc --version
61+
```
62+
You should see version information for Pandoc.
63+
64+
---
65+
66+
## How to Run
67+
68+
1. Save the script as `markdown_converter.py`.
69+
2. Run the program:
70+
```bash
71+
python markdown_converter.py
72+
```
73+
74+
---
75+
76+
## Usage
77+
78+
1. Click **"Select Markdown File"** in the app window.
79+
2. Choose a `.md` file from your system.
80+
3. Select output type: **PDF** or **Text**.
81+
4. Choose the save location and file name.
82+
5. Done! 🎉
83+
84+
---
85+
86+
## Unicode Support
87+
88+
- The PDF generation uses **HeiseiMin-W3** font to support a wide range of Unicode characters.
89+
- This ensures **Chinese, Japanese, Korean, and emoji** render correctly in the final PDF.
90+
91+
---
92+
93+
## Running Without Pandoc (Pure Python Option)
94+
95+
If you don’t want to install Pandoc,
96+
the code can be modified to use Python libraries like **markdown2** or **mistune** for parsing Markdown
97+
and **ReportLab** for PDF generation.
98+
99+
This removes the external dependency but keeps full functionality.
100+
101+
---
102+
103+
## Example Screenshots & GIF Demo (Optional)
104+
105+
You can add screenshots or a short screen recording here to make the README more user-friendly.
106+
107+
---
108+
109+
## License
110+
MIT License – free to use, modify, and share.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import sys
2+
from PySide6.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QFileDialog, QMessageBox
3+
from PySide6.QtCore import Qt
4+
import pypandoc
5+
from reportlab.platypus import SimpleDocTemplate, Paragraph
6+
from reportlab.lib.styles import getSampleStyleSheet
7+
from reportlab.pdfbase import pdfmetrics
8+
from reportlab.pdfbase.cidfonts import UnicodeCIDFont
9+
10+
class MarkdownConverter(QWidget):
11+
def __init__(self):
12+
super().__init__()
13+
self.setWindowTitle("Markdown Converter")
14+
self.setGeometry(300, 300, 400, 150)
15+
16+
layout = QVBoxLayout()
17+
self.button = QPushButton("Select Markdown File")
18+
self.button.clicked.connect(self.select_file)
19+
layout.addWidget(self.button)
20+
self.setLayout(layout)
21+
22+
# Register a Unicode font for PDF
23+
pdfmetrics.registerFont(UnicodeCIDFont('HeiseiMin-W3')) # Japanese font that supports wide Unicode range
24+
25+
def select_file(self):
26+
file_path, _ = QFileDialog.getOpenFileName(self, "Open Markdown File", "", "Markdown Files (*.md)")
27+
if file_path:
28+
self.convert_file(file_path)
29+
30+
def convert_file(self, file_path):
31+
save_type, _ = QFileDialog.getSaveFileName(
32+
self, "Save File As", "", "PDF Files (*.pdf);;Text Files (*.txt)"
33+
)
34+
if save_type:
35+
if save_type.endswith(".pdf"):
36+
self.convert_to_pdf(file_path, save_type)
37+
elif save_type.endswith(".txt"):
38+
self.convert_to_text(file_path, save_type)
39+
else:
40+
QMessageBox.warning(self, "Error", "Please select a valid file type.")
41+
42+
def convert_to_text(self, md_path, output_path):
43+
try:
44+
with open(md_path, "r", encoding="utf-8") as f:
45+
md_content = f.read()
46+
output = pypandoc.convert_text(md_content, 'plain', format='md', extra_args=['--standalone'])
47+
with open(output_path, "w", encoding="utf-8") as f:
48+
f.write(output)
49+
QMessageBox.information(self, "Success", "Markdown converted to Text successfully!")
50+
except Exception as e:
51+
QMessageBox.critical(self, "Error", f"Failed to convert: {e}")
52+
53+
def convert_to_pdf(self, md_path, output_path):
54+
try:
55+
with open(md_path, "r", encoding="utf-8") as f:
56+
md_content = f.read()
57+
text = pypandoc.convert_text(md_content, 'plain', format='md', extra_args=['--standalone'])
58+
doc = SimpleDocTemplate(output_path)
59+
styles = getSampleStyleSheet()
60+
story = [Paragraph(line, styles["Normal"]) for line in text.split("\n")]
61+
doc.build(story)
62+
QMessageBox.information(self, "Success", "Markdown converted to PDF successfully!")
63+
except Exception as e:
64+
QMessageBox.critical(self, "Error", f"Failed to convert: {e}")
65+
66+
67+
if __name__ == "__main__":
68+
app = QApplication(sys.argv)
69+
window = MarkdownConverter()
70+
window.show()
71+
sys.exit(app.exec())

SAK Utility/readme.pdf

4.93 KB
Binary file not shown.

0 commit comments

Comments
 (0)