import json from datetime import datetime def generate_metadata_table(trip_report): metadata_table = """
Attribute Value
Chemical {}
Tester {}
Dosage {}
Tolerance {}
Cross-Tolerance {}
""".format(trip_report['chemical'], trip_report['tester'], trip_report['dosage'], trip_report['tolerance'], trip_report['cross_tolerance']) return metadata_table def generate_trip_report_table(trip_report): trip_report_table = """ """ for entry in trip_report['entries']: trip_report_table += """ """.format(entry['timestamp'], entry['elapsed_time'], entry['notes']) trip_report_table += """
Timestamp Elapsed Time Notes
{} {} {}
""" return trip_report_table def generate_html_report(trip_report): metadata_table = generate_metadata_table(trip_report) trip_report_table = generate_trip_report_table(trip_report) submit_date = trip_report.get('submit_date', 'N/A') generation_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S") html_report = """

Metadata

{}

Report

{}

Report submitted at: {}

Static HTML generated at: {}

""".format(metadata_table, trip_report_table, submit_date, generation_date) return html_report def save_html_report(html_report, destination_path): with open(destination_path, 'w') as html_file: html_file.write(html_report) print("HTML report saved to {}.".format(destination_path)) if __name__ == "__main__": json_path = input("Enter path to JSON report: ") destination_path = input("Enter path for destination HTML file: ") with open(json_path, 'r') as json_file: trip_report = json.load(json_file) html_report = generate_html_report(trip_report) save_html_report(html_report, destination_path)