Python PyQt5 Insert Image in Notepad Text Editor Using QTextEdit Widget Full Tutorial For Beginners

Python PyQt5 Insert Image in Notepad Text Editor Using QTextEdit Widget Full Tutorial For Beginners

 

Welcome folks today in this tutorial we will be inserting image in text editor using QTextEdit Widget in PyQt5 library in python. All the full source code of the application will be given below.

 

 

 

Get Started

 

 

 

In order to get started we need to install the following library into your python project by using the pip command

 

pip install pyqt5

 

After you install this library make an app.py file and copy paste the following code

 

app.py

 

 

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QTextEdit
from PyQt5.QtGui import QTextCursor

class AppDemo(QWidget):
    def __init__(self):
        super().__init__()
        self.resize(1200, 800)

        mainLayout = QVBoxLayout()

        self.textEditor = QTextEdit()
        mainLayout.addWidget(self.textEditor)

        document = self.textEditor.document()
        cursor = QTextCursor(document)

        p1 = cursor.position() # returns int
        cursor.insertImage('profile.jpg')

        self.setLayout(mainLayout)
        

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

 

See also  Python 3 Bulk Domain Age Checker Web Scraping Script Using BeautifulSoup4 Library Full Project For Beginners

 

Here you need to give the full path of the image that you are inserting and then run the python script by executing the below command

 

python app.py

 

 

 

 

As you can see the image is successfully inserted along side with text inside the notepad text editor

 

Leave a Reply