0% found this document useful (0 votes)
2 views4 pages

MTM Universal Interface - Py

The document is a Python script that implements a GUI application using PyQt5 for generating reports based on data extracted from Excel and DAT files. It includes functionality for manual and automatic report generation, with progress updates and error handling. The application uses threading to manage report generation without freezing the UI.

Uploaded by

Bichi man
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

MTM Universal Interface - Py

The document is a Python script that implements a GUI application using PyQt5 for generating reports based on data extracted from Excel and DAT files. It includes functionality for manual and automatic report generation, with progress updates and error handling. The application uses threading to manage report generation without freezing the UI.

Uploaded by

Bichi man
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

import sys

import os
import re
from PyQt5.QtWidgets import (
QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton,
QLineEdit, QTextEdit, QFileDialog, QTabWidget, QMessageBox, QGroupBox
)
from PyQt5.QtCore import Qt, QThread, pyqtSignal
from MTM_Extract_Function import extraire_donnees_use_case, generer_tableau_html

class ReportGenerator(QThread):
progress_update = pyqtSignal(str)
report_generated = pyqtSignal(str, str)
error_occurred = pyqtSignal(str)

def __init__(self, data_file, uc_number=None, dat_file=None):


super().__init__()
self.data_file = data_file
self.uc_number = uc_number
self.dat_file = dat_file

def run(self):
try:
if self.uc_number:
self.progress_update.emit(f"Extraction UC {self.uc_number}...")
donnees = extraire_donnees_use_case(self.data_file, self.uc_number)
html = generer_tableau_html(donnees, chemin_test=self.data_file)
path = f"Rapport_MTM_UC{self.uc_number}.html"
with open(path, "w", encoding="utf-8") as f:
f.write(html)
self.report_generated.emit(str(self.uc_number), path)
elif self.dat_file:
match = re.search(r'UC(\d+)', os.path.basename(self.dat_file))
if not match:
self.error_occurred.emit("Numéro UC non détecté dans le nom du
fichier")
return
uc_number = int(match.group(1))
self.progress_update.emit(f"UC {uc_number} détecté")
donnees = extraire_donnees_use_case(self.data_file, uc_number)
html = generer_tableau_html(donnees, chemin_test=self.dat_file)
path = f"Rapport_Auto_UC{uc_number}.html"
with open(path, "w", encoding="utf-8") as f:
f.write(html)
self.report_generated.emit(str(uc_number), path)
except Exception as e:
self.error_occurred.emit(str(e))

class MTMUniversalInterface(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("MTM Universal Interface - Générateur de Rapports")
self.setGeometry(100, 100, 700, 500)
self.setStyleSheet("background-color: #f7f9fc;")

self.data_file = None
self.dat_file = None

layout = QVBoxLayout()
tabs = QTabWidget()
tabs.setStyleSheet("QTabBar::tab { background: #e0e0e0; padding: 10px; }
QTabBar::tab:selected { background: #005BAC; color: white; }")

manual_tab = QWidget()
manual_layout = QVBoxLayout()

group_data = QGroupBox("Configuration des données")


group_data_layout = QVBoxLayout()
self.excel_label = QLabel("Aucun fichier Excel sélectionné")
self.excel_label.setStyleSheet("color: #333; font-weight: bold;")
excel_btn = QPushButton("Sélectionner CSV/Excel")
excel_btn.setStyleSheet("background-color: #005BAC; color: white; padding:
8px; border-radius: 5px;")
excel_btn.clicked.connect(self.select_excel)
group_data_layout.addWidget(excel_btn)
group_data_layout.addWidget(self.excel_label)
group_data.setLayout(group_data_layout)
manual_layout.addWidget(group_data)

group_gen = QGroupBox("Génération de rapport manuel")


group_gen_layout = QHBoxLayout()
self.uc_input = QLineEdit()
self.uc_input.setPlaceholderText("Use Case")
self.uc_input.setStyleSheet("padding: 6px; border: 1px solid #ccc; border-
radius: 4px;")
manual_btn = QPushButton("Générer rapport")
manual_btn.setStyleSheet("background-color: #007A2F; color: white; padding:
8px; border-radius: 5px;")
manual_btn.clicked.connect(self.generate_manual)
group_gen_layout.addWidget(QLabel("Use Case:"))
group_gen_layout.addWidget(self.uc_input)
group_gen_layout.addWidget(manual_btn)
group_gen.setLayout(group_gen_layout)
manual_layout.addWidget(group_gen)
manual_tab.setLayout(manual_layout)

auto_tab = QWidget()
auto_layout = QVBoxLayout()
group_auto = QGroupBox("Génération automatique depuis fichier DAT")
group_auto_layout = QVBoxLayout()
self.dat_label = QLabel("Aucun fichier DAT sélectionné")
self.dat_label.setStyleSheet("color: #333; font-weight: bold;")
dat_btn = QPushButton("Sélectionner fichier .dat")
dat_btn.setStyleSheet("background-color: #ff9800; color: white; padding:
8px; border-radius: 5px;")
dat_btn.clicked.connect(self.select_dat)
auto_btn = QPushButton("Générer rapport auto")
auto_btn.setStyleSheet("background-color: #9b59b6; color: white; padding:
8px; border-radius: 5px;")
auto_btn.clicked.connect(self.generate_auto)
group_auto_layout.addWidget(dat_btn)
group_auto_layout.addWidget(self.dat_label)
group_auto_layout.addWidget(auto_btn)
group_auto.setLayout(group_auto_layout)
auto_layout.addWidget(group_auto)
auto_tab.setLayout(auto_layout)

tabs.addTab(manual_tab, "Mode Manuel")


tabs.addTab(auto_tab, "Mode Automatique")
layout.addWidget(tabs)

group_status = QGroupBox("Progression et résultats")


group_status_layout = QVBoxLayout()
self.status = QTextEdit()
self.status.setReadOnly(True)
self.status.setStyleSheet("background-color: #f5f7fa; border: 1px solid
#ccc; border-radius: 5px;")
group_status_layout.addWidget(self.status)
group_status.setLayout(group_status_layout)
layout.addWidget(group_status)

self.setLayout(layout)

def select_excel(self):
path, _ = QFileDialog.getOpenFileName(self, "Excel", "", "Excel Files
(*.xlsx *.xls)")
if path:
self.data_file = path
self.excel_label.setText(os.path.basename(path))
self.status.append(f"Excel sélectionné : {path}")

def select_dat(self):
path, _ = QFileDialog.getOpenFileName(self, "DAT", "", "DAT Files (*.dat)")
if path:
self.dat_file = path
self.dat_label.setText(os.path.basename(path))
self.status.append(f"Fichier DAT sélectionné : {path}")

def generate_manual(self):
if not self.data_file:
QMessageBox.warning(self, "Erreur", "Veuillez sélectionner un fichier
Excel.")
return
if not self.uc_input.text().isdigit():
QMessageBox.warning(self, "Erreur", "Veuillez entrer un numéro UC
valide.")
return
uc = int(self.uc_input.text())
self.worker = ReportGenerator(self.data_file, uc_number=uc)
self.connect_worker()

def generate_auto(self):
if not self.dat_file:
QMessageBox.warning(self, "Erreur", "Veuillez sélectionner un fichier
DAT.")
return
if not self.data_file:
QMessageBox.warning(self, "Erreur", "Veuillez d'abord sélectionner un
fichier Excel dans l'onglet manuel.")
return
self.worker = ReportGenerator(self.data_file, dat_file=self.dat_file)
self.connect_worker()

def connect_worker(self):
self.worker.progress_update.connect(self.status.append)
self.worker.report_generated.connect(self.show_report)
self.worker.error_occurred.connect(self.show_error)
self.worker.start()

def show_report(self, uc, path):


self.status.append(f"Rapport UC {uc} généré : {path}")
try:
os.startfile(path)
except:
pass

def show_error(self, msg):


self.status.append(f"Erreur : {msg}")

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

if __name__ == "__main__":
main()

You might also like