display_json angelegt

This commit is contained in:
Czechman 2025-02-16 17:59:23 +01:00
parent b854dfa567
commit 64f38cb5c7
1 changed files with 61 additions and 0 deletions

61
display_json.py Normal file
View File

@ -0,0 +1,61 @@
#!/usr/bin/env python3
import json
import argparse
def pretty_print_json(data):
"""
Gibt die JSON-Daten als schön formatierten String zurück.
"""
return json.dumps(data, indent=2, ensure_ascii=False)
def write_html_file(pretty_json_str, output_file):
"""
Schreibt den formatierten JSON-String in eine HTML-Datei,
wobei er in einem <pre>-Block angezeigt wird.
"""
html_content = f"""<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSON Display</title>
<style>
body {{
font-family: monospace;
white-space: pre;
background-color: #f8f8f8;
padding: 20px;
}}
</style>
</head>
<body>
{pretty_json_str}
</body>
</html>"""
with open(output_file, "w", encoding="utf-8") as f:
f.write(html_content)
def main():
parser = argparse.ArgumentParser(
description="Lädt eine JSON-Datei, zeigt sie in der Konsole an und speichert sie als HTML."
)
parser.add_argument("input", help="Pfad zur Eingabe-JSON-Datei.")
parser.add_argument("-o", "--output", default="output.html",
help="Pfad zur Ausgabe-HTML-Datei (Standard: output.html).")
args = parser.parse_args()
# JSON-Datei laden
with open(args.input, "r", encoding="utf-8") as f:
data = json.load(f)
# JSON schön formatieren
pretty_json_str = pretty_print_json(data)
# In der Konsole ausgeben
print(pretty_json_str)
# In HTML-Datei schreiben
write_html_file(pretty_json_str, args.output)
print(f"\nHTML-Ausgabe gespeichert in '{args.output}'.")
if __name__ == "__main__":
main()