In this Python article we want to learn How to Export File as PDF in Python, for this purpose we are going to use PySide6 GUI Framework, so PySide6 is Python binding for Qt framework, which is popular C++ framework for building desktop applications. with PySide6 you can create beautiful and functional graphical user interfaces (GUIs) for your applications. in this article we want to learn how to export a file as PDF in PySide6.
How to Export File as PDF in Python
First of all we need to install this library, and for that we can use pip
1 |
pip install PySide6 |
After installation we need to create our simple GUI application with Python PySide6.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import sys from PySide6.QtWidgets import QApplication, QMainWindow, QTextEdit class MainWindow(QMainWindow): def __init__(self): super().__init__() self.text_edit = QTextEdit(self) self.setCentralWidget(self.text_edit) app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec()) |
If you run the code you will see simple GUI Window in Python.
Now that we have basic GUI let’s add menu item that allows the user to save their file as PDF. we do this by creating new menu item in the File menu:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import sys from PySide6.QtWidgets import QApplication, QMainWindow, QTextEdit, QFileDialog from PySide6.QtGui import QAction class MainWindow(QMainWindow): def __init__(self): super().__init__() self.text_edit = QTextEdit(self) self.setCentralWidget(self.text_edit) save_as_pdf_action = QAction("Save as PDF", self) save_as_pdf_action.triggered.connect(self.save_as_pdf) file_menu = self.menuBar().addMenu("File") file_menu.addAction(save_as_pdf_action) def save_as_pdf(self): file_path, _ = QFileDialog.getSaveFileName(self, "Save as PDF", "", "PDF Files (*.pdf)") if file_path: pass app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec()) |
This code creates new menu item called Save as PDF and connects it to new method called save_as_pdf(). this method is called whenever the user clicks on the Save as PDF menu item.
Run the code and you will see a menu item
Now that we have a menu item that allows the user to save their file as a PDF, let’s implement save_as_pdf() method. we do this using the QPrinter class which is part of the Qt framework:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import sys from PySide6.QtWidgets import QMainWindow, QTextEdit, QFileDialog from PySide6.QtGui import QAction, QPdfWriter from PySide6.QtPrintSupport import QPrinter class MainWindow(QMainWindow): def __init__(self): super().__init__() self.text_edit = QTextEdit(self) self.setCentralWidget(self.text_edit) save_as_pdf_action = QAction("Save as PDF", self) save_as_pdf_action.triggered.connect(self.save_as_pdf) file_menu = self.menuBar().addMenu("File") file_menu.addAction(save_as_pdf_action) def save_as_pdf(self): file_path, _ = QFileDialog.getSaveFileName(self, "Save as PDF", "", "PDF Files (*.pdf)") if file_path: printer = QPrinter(QPrinter.HighResolution) printer.setOutputFormat(QPrinter.PdfFormat) printer.setOutputFileName(file_path) writer = QPdfWriter(file_path) self.text_edit.document().print_(printer) |
In the above code we first get the file path using QFileDialog. if the user selects a file path, we creates new QPrinter object and set its output format to PDF using setOutputFormat(QPrinter.PdfFormat). after that we set the output file name using setOutputFileName(file_path).
next we creates new QPdfWriter object with the same file path, which we will use to create the PDF file. and at the end we print the contents of the text editor widget to the PDF file using self.text_edit.document().print_(printer).
So now this is the complete code for this article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import sys from PySide6.QtWidgets import QApplication, QMainWindow, QTextEdit, QFileDialog from PySide6.QtGui import QPdfWriter, QAction from PySide6.QtPrintSupport import QPrinter class MainWindow(QMainWindow): def __init__(self): super().__init__() self.text_edit = QTextEdit(self) self.setCentralWidget(self.text_edit) save_as_pdf_action = QAction("Save as PDF", self) save_as_pdf_action.triggered.connect(self.save_as_pdf) file_menu = self.menuBar().addMenu("File") file_menu.addAction(save_as_pdf_action) def save_as_pdf(self): file_path, _ = QFileDialog.getSaveFileName(self, "Save as PDF", "", "PDF Files (*.pdf)") if file_path: printer = QPrinter(QPrinter.HighResolution) printer.setOutputFormat(QPrinter.PdfFormat) printer.setOutputFileName(file_path) writer = QPdfWriter(file_path) self.text_edit.document().print_(printer) app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec()) |
Run the complete code write something in QTextEdit and you can export that as PDF in Python.
Learn More on Python GUI
- How to Create Print Dialog in PySide6
- How to Create Print Preview in PySide6
- How to Use Qt Designer with PySide6
- How to Add Icon to PySide6 Window
- How to Load UI in Python PySide6
- How to Create RadioButton in PySide6
- How to Create ComboBox in PySide6
- How to Create CheckBox in Python PySide6
- Responsive Applications with PyQt6 Multithreading
- Event Handling in Python and PyQt6
- How to Use Stylesheets in Python PyQt6