Today I make a simple tool to clean the HTML from style and more. I used artificial inteligence from copilot.
This is the result of this simple tool.

from PyQt6.QtWidgets import QApplication, QMainWindow, QTextEdit, QMenu
from bs4 import BeautifulSoup
class HtmlEditor(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("catafest-cleaner-HTML") # Titlu actualizat
self.setGeometry(100, 100, 800, 600)
# Editor de text
self.editor = QTextEdit(self)
self.setCentralWidget(self.editor)
# Meniu contextual
self.editor.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
self.editor.customContextMenuRequested.connect(self.show_context_menu)
def show_context_menu(self, position):
menu = QMenu(self)
clean_styles_action = menu.addAction("Clean basic HTML")
clean_styles_action.triggered.connect(self.clean_all_styles)
clean_empty_tags_action = menu.addAction("Clean Empty Tags")
clean_empty_tags_action.triggered.connect(self.clean_empty_tags)
menu.exec(self.editor.mapToGlobal(position))
def clean_all_styles(self):
# Obține conținutul HTML din editor
html_content = self.editor.toPlainText()
# Utilizează BeautifulSoup pentru a procesa HTML-ul
soup = BeautifulSoup(html_content, 'html.parser')
# Elimină toate atributele, cu excepția celor din ancorele <a>
for tag in soup.find_all(True):
if tag.name == "a": # Păstrează doar atributul 'href' pentru <a>
attrs_to_keep = {"href": tag.attrs.get("href")} if "href" in tag.attrs else {}
tag.attrs = attrs_to_keep
else:
tag.attrs = {} # Elimină toate atributele pentru celelalte tag-uri
# Actualizează conținutul editorului
clean_html = str(soup)
self.editor.setPlainText(clean_html)
def clean_empty_tags(self):
# Obține conținutul HTML din editor
html_content = self.editor.toPlainText()
# Utilizează BeautifulSoup pentru a procesa HTML-ul
soup = BeautifulSoup(html_content, 'html.parser')
# Elimină tag-urile goale
for tag in soup.find_all(True):
# Verifică tag-uri goale
if not tag.contents or all(str(content).strip() == "" for content in tag.contents):
tag.decompose() # Elimină tag-ul complet
# Actualizează conținutul editorului
clean_html = str(soup)
self.editor.setPlainText(clean_html)
if __name__ == "__main__":
import sys
from PyQt6.QtCore import Qt
app = QApplication(sys.argv)
window = HtmlEditor()
window.show()
sys.exit(app.exec())