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
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 |
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_()) |
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