|
| 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()) |
0 commit comments