Modern GUI: PyQt PySide Python for Displaying PDF and Other File Formats

Posted by


In this tutorial, we will be discussing how to display PDF files and other file formats using PyQt and PySide in Python. PyQt and PySide are Python bindings for the Qt application framework, which allows us to create modern and powerful graphical user interfaces (GUIs). Displaying PDF files and other file formats in a PyQt/PySide application can be a useful feature for applications that need to work with various types of documents.

To get started, we will need to have PyQt or PySide installed in our Python environment. You can install PyQt by using the following command:

pip install PyQt5

Or PySide by using the following command:

pip install PySide2

Next, we will need to create a new Python script and import the necessary modules:

import sys
from PySide2.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton, QFileDialog
from PySide2.QtCore import Qt
from PySide2.QtGui import QPixmap, QImage
from PySide2.QtPrintSupport import QPrinter, QPrintDialog
from PySide2.QtWebEngineWidgets import QWebEngineView

In this tutorial, we will create a simple PyQt/PySide application with a button that allows the user to load a PDF file and display it in a widget. We will also add functionality to print the document using the QPrintDialog.

Let’s start by creating a basic window with a QLabel and a QPushButton to load the PDF file:

class PDFViewer(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("PDF Viewer")
        self.setGeometry(100, 100, 800, 600)

        self.label = QLabel(self)
        self.label.setGeometry(10, 10, 780, 580)

        self.button = QPushButton("Load PDF", self)
        self.button.setGeometry(10, 10, 100, 30)
        self.button.clicked.connect(self.load_pdf)

    def load_pdf(self):
        options = QFileDialog.Options()
        fileName, _ = QFileDialog.getOpenFileName(self, "Open PDF File",
                                                  "", "PDF Files (*.pdf);;All Files (*)", options=options)
        if fileName:
            self.display_pdf(fileName)

    def display_pdf(self, file_path):
        self.webview = QWebEngineView()
        self.webview.load(QUrl.fromLocalFile(file_path))
        self.setCentralWidget(self.webview)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = PDFViewer()
    window.show()
    sys.exit(app.exec_())

In the PDFViewer class, we create a window with a QLabel and a QPushButton. When the user clicks the button, the load_pdf method is called, which opens a file dialog for selecting a PDF file. If a file is selected, the display_pdf method is called, which creates a QWebEngineView widget and loads the selected PDF file into it.

You can customize the appearance and functionality of the PDF viewer by adding additional features such as zoom controls, search functionality, and navigation buttons. You can also add the ability to display other file formats such as images, text files, and HTML files by using appropriate Qt widgets and classes.

To print the displayed PDF file, you can add a button to the window that triggers a print dialog when clicked. Here is an example of how to implement a print functionality in the PDFViewer class:

def print_pdf(self):
    printer = QPrinter(QPrinter.HighResolution)
    dialog = QPrintDialog(printer, self)

    if dialog.exec_():
        self.webview.print_(printer)

You can connect this method to a QPushButton and add it to the window to allow the user to print the displayed PDF file.

In this tutorial, we have covered how to display PDF files and other file formats using PyQt and PySide in Python. You can expand on this example by adding more features and functionality to create a custom file viewer application. PyQt and PySide provide a wide range of widgets and classes for working with different types of files and documents, making it easy to create versatile and user-friendly GUI applications.

0 0 votes
Article Rating

Leave a Reply

10 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@SpinnTV
2 hours ago
@_isDev
2 hours ago

how do you able the zoom button? thanks

@ScottCov
2 hours ago

How do I load a pdf file? When I click File, it just adds blank tab. Also for some reason when I load the africau image I don't have the option to zoom in or out?

@lucianamachadolins3304
2 hours ago

Your video helped me a lot! But I have some doubts…
Through this PDF browser can I know which page the pdf is on? I would like to create this navigation through other buttons

@PANDURANG99
2 hours ago

What about annotations and highlights

@MARIO-tb3tr
2 hours ago

Does this tool is going to work with piside6 o just with pyside2?

@stephanesimard8602
2 hours ago

Hi Spinn TV,

I am currently learning how to use PyQt for an automotive display and I have been using your AnalogGaugeWidget code to learn. In your tutorial videos, you show the Gauge being edited by a side menu in the Qt mainwindow and not from the source code. I was wondering if you provide the code for the editing of the gauge in your GitHub, as I am trying to figure out how to change widgets using other widgets with little to no progress.

Thank you for your work in PyQt, I haven't found anything else this complex and detailed!

@turtlecode
2 hours ago

Perfect tutorial!

@tfprime7386
2 hours ago

Spinn TV,

First I would like to congratulate you for the channel. The content of the video is very usefull.

Could I give you a video suggestion? I think it can be useful for the whole community that follows you.

Make a video how to add Qt Advanced Docking System or KDDockWidgets on Desktop GUI developed here, on your channel.

A lot of success for you.

@mergenstudios8779
2 hours ago

ayy

10
0
Would love your thoughts, please comment.x
()
x