mirror of
https://forge.chapril.org/tykayn/parking-land
synced 2025-06-20 01:44:42 +02:00
99 lines
No EOL
3.4 KiB
Python
99 lines
No EOL
3.4 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
Script simplifié pour convertir un fichier HTML en image JPG.
|
|
Utilise playwright pour rendre la page web et la capturer en image.
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import argparse
|
|
from pathlib import Path
|
|
|
|
def convert_html_to_jpg(html_path, output_path=None):
|
|
"""
|
|
Convertit un fichier HTML en image JPG en utilisant playwright.
|
|
|
|
Args:
|
|
html_path (str): Chemin vers le fichier HTML à convertir
|
|
output_path (str, optional): Chemin où sauvegarder l'image JPG
|
|
|
|
Returns:
|
|
str: Chemin vers l'image JPG générée ou None en cas d'erreur
|
|
"""
|
|
if not os.path.exists(html_path):
|
|
print(f"Erreur: Le fichier HTML n'existe pas: {html_path}")
|
|
return None
|
|
|
|
# Définir le chemin de sortie si non spécifié
|
|
if not output_path:
|
|
output_path = os.path.splitext(html_path)[0] + '.jpg'
|
|
|
|
try:
|
|
# Vérifier si playwright est installé
|
|
try:
|
|
from playwright.sync_api import sync_playwright
|
|
except ImportError:
|
|
print("Erreur: playwright n'est pas installé.")
|
|
print("Installez-le avec: pip install playwright")
|
|
print("Puis exécutez: playwright install")
|
|
return None
|
|
|
|
# Obtenir le chemin absolu du fichier HTML
|
|
abs_path = os.path.abspath(html_path)
|
|
file_url = f"file://{abs_path}"
|
|
|
|
print(f"Conversion de {html_path} en {output_path}...")
|
|
|
|
# Utiliser playwright pour capturer la page
|
|
with sync_playwright() as p:
|
|
# Lancer un navigateur
|
|
browser = p.chromium.launch()
|
|
page = browser.new_page(viewport={"width": 1200, "height": 800})
|
|
|
|
# Naviguer vers la page HTML
|
|
page.goto(file_url)
|
|
|
|
# Attendre que la page soit chargée
|
|
page.wait_for_load_state("networkidle")
|
|
|
|
# Attendre un peu plus pour les cartes qui peuvent prendre du temps à se charger
|
|
page.wait_for_timeout(2000)
|
|
|
|
# Prendre une capture d'écran
|
|
page.screenshot(path=output_path, full_page=True)
|
|
|
|
# Fermer le navigateur
|
|
browser.close()
|
|
|
|
print(f"Conversion réussie: {output_path}")
|
|
return output_path
|
|
|
|
except Exception as e:
|
|
print(f"Erreur lors de la conversion: {str(e)}")
|
|
|
|
# Proposer des solutions alternatives
|
|
print("Suggestions pour résoudre le problème:")
|
|
print("1. Installez playwright: pip install playwright")
|
|
print("2. Exécutez: playwright install")
|
|
print("3. Utilisez un navigateur pour ouvrir le fichier HTML et faites une capture d'écran manuellement")
|
|
|
|
return None
|
|
|
|
def main():
|
|
"""
|
|
Fonction principale qui traite les arguments de ligne de commande
|
|
et convertit le fichier HTML en JPG.
|
|
"""
|
|
parser = argparse.ArgumentParser(description='Convertit un fichier HTML en image JPG.')
|
|
parser.add_argument('html_path', type=str, help='Chemin vers le fichier HTML à convertir')
|
|
parser.add_argument('-o', '--output', type=str, help='Chemin où sauvegarder l\'image JPG')
|
|
|
|
args = parser.parse_args()
|
|
|
|
# Convertir le fichier HTML en JPG
|
|
convert_html_to_jpg(args.html_path, args.output)
|
|
|
|
if __name__ == "__main__":
|
|
main() |