79 lines
3.1 KiB
Python
79 lines
3.1 KiB
Python
import subprocess
|
|
import hub.helpers.constants as cte
|
|
import matplotlib.pyplot as plt
|
|
import random
|
|
import matplotlib.colors as mcolors
|
|
from matplotlib import cm
|
|
import datetime
|
|
|
|
class LatexReport:
|
|
def __init__(self, file_name):
|
|
self.file_name = file_name
|
|
self.content = []
|
|
self.content.append(r'\documentclass{article}')
|
|
self.content.append(r'\usepackage[margin=2.5cm]{geometry}') # Adjust page margins
|
|
self.content.append(r'\usepackage{graphicx}')
|
|
self.content.append(r'\usepackage{tabularx}')
|
|
self.content.append(r'\begin{document}')
|
|
# Get current date and time
|
|
current_datetime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
self.content.append(r'\title{Energy System Analysis Report - ' + current_datetime + r'}')
|
|
self.content.append(r'\author{Next-Generation Cities Institute}')
|
|
self.content.append(r'\date{}') # Remove the date field, as it's included in the title now
|
|
self.content.append(r'\maketitle')
|
|
|
|
def add_section(self, section_title):
|
|
self.content.append(r'\section{' + section_title + r'}')
|
|
|
|
def add_subsection(self, subsection_title):
|
|
self.content.append(r'\subsection{' + subsection_title + r'}')
|
|
|
|
def add_text(self, text):
|
|
self.content.append(text)
|
|
|
|
def add_table(self, table_data, caption=None, first_column_width=None):
|
|
num_columns = len(table_data[0])
|
|
total_width = 0.9 # Default total width
|
|
|
|
if first_column_width is not None:
|
|
first_column_width_str = str(first_column_width) + 'cm'
|
|
total_width -= first_column_width / 16.0 # Adjust total width for the first column
|
|
|
|
if caption:
|
|
self.content.append(r'\begin{table}[htbp]')
|
|
self.content.append(r'\caption{' + caption + r'}')
|
|
self.content.append(r'\centering')
|
|
|
|
self.content.append(r'\begin{tabularx}{\textwidth}{|p{' + first_column_width_str + r'}|' + '|'.join(['X'] * (
|
|
num_columns - 1)) + '|}' if first_column_width is not None else r'\begin{tabularx}{\textwidth}{|' + '|'.join(
|
|
['X'] * num_columns) + '|}')
|
|
self.content.append(r'\hline')
|
|
for row in table_data:
|
|
self.content.append(' & '.join(row) + r' \\')
|
|
self.content.append(r'\hline')
|
|
self.content.append(r'\end{tabularx}')
|
|
|
|
if caption:
|
|
self.content.append(r'\end{table}')
|
|
|
|
def add_image(self, image_path, caption=None):
|
|
if caption:
|
|
self.content.append(r'\begin{figure}[htbp]')
|
|
self.content.append(r'\centering')
|
|
self.content.append(r'\includegraphics[width=0.8\textwidth]{' + image_path + r'}')
|
|
self.content.append(r'\caption{' + caption + r'}')
|
|
self.content.append(r'\end{figure}')
|
|
else:
|
|
self.content.append(r'\begin{figure}[htbp]')
|
|
self.content.append(r'\centering')
|
|
self.content.append(r'\includegraphics[width=0.8\textwidth]{' + image_path + r'}')
|
|
self.content.append(r'\end{figure}')
|
|
|
|
def save_report(self):
|
|
self.content.append(r'\end{document}') # Add this line to close the document
|
|
with open(self.file_name, 'w') as f:
|
|
f.write('\n'.join(self.content))
|
|
|
|
def compile_to_pdf(self):
|
|
subprocess.run(['pdflatex', self.file_name])
|