In this Python PySide6 lesson we are going to learn about Python PySide6 PDF Exporter App, for doing this functionality we are going to use QPrintDialog.
To use QPrintDialog for exporting a file as PDF you can follow these steps:
Import the necessary modules:
1 2 3 |
from PySide6.QtWidgets import QApplication, QFileDialog, QTextEdit, QMainWindow from PySide6.QtGui import QPdfWriter, QAction from PySide6.QtPrintSupport import QPrintDialog |
Create QMainWindow object:
1 2 3 |
class MyWindow(QMainWindow): def __init__(self): super().__init__() |
Create QTextEdit widget to hold the text you want to export as PDF:
1 2 |
self.text_edit = QTextEdit() self.setCentralWidget(self.text_edit) |
Create QAction object for exporting the file as PDF and add it to the menu bar:
1 2 3 4 5 |
self.export_action = QAction("Export as PDF", self) self.export_action.setShortcut("Ctrl+E") self.export_action.triggered.connect(self.export_as_pdf) self.file_menu = self.menuBar().addMenu("File") self.file_menu.addAction(self.export_action) |
Create a method to handle the exporting of the file as PDF:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
def export_as_pdf(self): # Show print dialog print_dialog = QPrintDialog(self) if not print_dialog.exec(): return # Get filename to save the PDF to file_name, _ = QFileDialog.getSaveFileName(self, "Export PDF", "", "PDF Files (*.pdf);;All Files (*)") if not file_name: return # Create QPdfWriter object to write the PDF to the specified file pdf_writer = QPdfWriter(file_name) # Print document to the QPdfWriter object self.text_edit.document().print_(pdf_writer) |
In the export_as_pdf method the QPrintDialog is shown to allow the user to select printer and other print options. If the user cancels the dialog the method returns without exporting the file.
If the user selects printer, the getSaveFileName method of QFileDialog is called to allow the user to choose the filename and location to save the PDF file. If the user cancels this dialog, the method returns without exporting the file.
A QPdfWriter object is created with the filename chosen by the user.
Finally the print_ method of the QTextEdit widget’s document object is called to print the document to the QPdfWriter object.
This is the complete code
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 31 32 33 34 35 36 37 38 39 40 41 |
from PySide6.QtWidgets import QApplication, QFileDialog, QTextEdit, QMainWindow from PySide6.QtGui import QPdfWriter, QAction from PySide6.QtPrintSupport import QPrintDialog class MyWindow(QMainWindow): def __init__(self): super().__init__() # Create text editor widget self.text_edit = QTextEdit() self.setCentralWidget(self.text_edit) # Create export action and add it to menu bar self.export_action = QAction("Export as PDF", self) self.export_action.setShortcut("Ctrl+E") self.export_action.triggered.connect(self.export_as_pdf) self.file_menu = self.menuBar().addMenu("File") self.file_menu.addAction(self.export_action) def export_as_pdf(self): # Show print dialog print_dialog = QPrintDialog(self) if not print_dialog.exec(): return # Get filename to save the PDF to file_name, _ = QFileDialog.getSaveFileName(self, "Export PDF", "", "PDF Files (*.pdf);;All Files (*)") if not file_name: return # Create QPdfWriter object to write the PDF to the specified file pdf_writer = QPdfWriter(file_name) # Print document to the QPdfWriter object self.text_edit.document().print_(pdf_writer) if __name__ == "__main__": app = QApplication([]) window = MyWindow() window.show() app.exec() |
Run the code and this will be the result.