analitics

Pages

Sunday, December 26, 2021

Python 3.7.11 : My colab tutorials - part 022.

Here is another notebook with two python scripts.
You may be wondering why I add them here and the index of 022 blog posts does not match the 026 index of those on the GitHub website.
The answer is simple: here I post them when I have time for evaluation and there they are added when they are created and tested.
The posts here are for a share of those who want to learn simple python programming to solve common issues by anyone with minimal school knowledge and for supporting the python programming community.
In this notebook you will find a script that uses a python packet that does a simple search using Google and one that does an image search.

Tuesday, December 7, 2021

Python Qt5 : Simple browser with QWebEngineView.

This is a simple example with PyQt5 and QWebEngineView.
You can see this example and use it from my GitHub account.

Monday, December 6, 2021

Python Qt5 : QtWebEngineWidgets and button.

In this tutorial I will show you how to add a simple button to the application build with QtWebEngineWidgets using the addWidget to a QVBoxLayout with a lay variable.
The button named PyQt5 nothing button is not connected.
Let's see this default example:
import sys

from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication, QPushButton, QMainWindow, QVBoxLayout, QWidget
from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineSettings, QWebEngineView

class Widget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.view = QWebEngineView()
        lay = QVBoxLayout(self)
        lay.addWidget(self.view)
        self.resize(640, 480)

class WebEnginePage(QWebEnginePage):
    def createWindow(self, _type):
        w = Widget()
        w.show()
        return w.view.page()

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent=None)

        self.setWindowTitle("PyQt5 ... another example !!")
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        
        button = QPushButton('PyQt5 nothing button', self)
        button.setToolTip('This is an example button')

        self.webview = QWebEngineView()
        self.page = WebEnginePage()
        self.webview.setPage(self.page)
        self.webview.settings().setAttribute(
            QWebEngineSettings.FullScreenSupportEnabled, True
        )

        self.webview.load(
            QUrl("https://www.youtube.com/watch?v=BVT3acNFzqc?rel=0&showinfo=0")
        )

        lay = QVBoxLayout(central_widget)
        lay.addWidget(button)
        lay.addWidget(self.webview)

def main():
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec_()

if __name__ == "__main__":
    main()

Saturday, December 4, 2021

Python 3.6.9 : Unarchive a RAR file with UnRAR and python.

In this tutorial, I will show you how can unrar a RAR archive with and without a password.
First, go to this website and install the UnRAR.dll file.
After you install into default path: C:\Program Files (x86)\UnrarDLL you need to create and set a new environment variable named UNRAR_LIB_PATH.
You need to select the 32 or 64 paths to this environment variable, and this depends on the test archive.
Because I create a RAR archive with x64 version I used this path for the environment variable: C:\Program Files (x86)\UnrarDLL\x64\UnRAR64.dll.
The first test archive I created was named TestRAR.rar and I used a password 111 with encryption.
The second one is named TestRAR001.rar and has no password.
Let's install the unrar python module with the pip tool.
pip install unrar
Requirement already satisfied: unrar in c:\python39\lib\site-packages (0.4)
Let's see the source code in python version 3.6.9
from unrar import rarfile

print("-----------------")
#archiveRARFile = r"C:\\PythonProjects\\RARArchive\\TestRAR.rar"
archiveRARFile = r"C:\\PythonProjects\\RARArchive\\TestRAR001.rar"
extractPath = r"C:\\PythonProjects\\RARArchive"

files = []
#with rarfile.RarFile(archiveRARFile,"r","111") as rarFile:
with rarfile.RarFile(archiveRARFile,"r","") as rarFile:
    files = rarFile.namelist()
    rarFile.extractall(extractPath)

print("files ",files)

for file in files:
    pathFile = fr"{extractPath}+{file}"
    with open(file,"r") as f:
        data = f.readlines()
        for line in data:
            line = line.strip()
            print(line)
You can see I commented two rows for each archive in order to test each one:
After I tested, this is the result of each test and both work great.
C:\PythonProjects\RARArchive>python unrarFile.py
-----------------
files  ['TestRAR.txt']
This is a test for RAR archive.

C:\PythonProjects\RARArchive>python unrarFile.py
-----------------
files  ['TestRAR.txt']
This is a test for RAR archive.