histlearn commited on
Commit
a144be4
·
verified ·
1 Parent(s): 2abc884

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -28
app.py CHANGED
@@ -137,7 +137,12 @@ def process_relatorios(contador_csv_path, relatorio_csv_path):
137
  contador_df.to_csv(relatorio_csv_path, index=False)
138
  return contador_df, media_tempo_medio_turma
139
 
140
- def generate_pdf_report(dataframe, media_tempo_medio_turma, output_pdf_path):
 
 
 
 
 
141
  class PDF(FPDF):
142
  def header(self):
143
  self.set_font('Arial', 'B', 12)
@@ -149,37 +154,12 @@ def generate_pdf_report(dataframe, media_tempo_medio_turma, output_pdf_path):
149
  self.set_font('Arial', 'I', 8)
150
  self.cell(0, 10, f'Page {self.page_no()}', 0, 0, 'C')
151
 
152
- def add_table(self, dataframe):
153
- self.set_font('Arial', 'B', 10)
154
- col_width = self.w / len(dataframe.columns)
155
- row_height = self.font_size
156
-
157
- # Adiciona os cabeçalhos
158
- for col in dataframe.columns:
159
- self.cell(col_width, row_height * 2, col, border=1)
160
- self.ln(row_height * 2)
161
-
162
- # Adiciona os dados com quebras de página
163
- self.set_font('Arial', '', 10)
164
- for row in dataframe.itertuples(index=False):
165
- for item in row:
166
- self.cell(col_width, row_height * 2, str(item), border=1)
167
- self.ln(row_height * 2)
168
- if self.get_y() > self.page_break_trigger - 2 * row_height:
169
- self.add_page()
170
- self.set_font('Arial', 'B', 10)
171
- for col in dataframe.columns:
172
- self.cell(col_width, row_height * 2, col, border=1)
173
- self.ln(row_height * 2)
174
- self.set_font('Arial', '', 10)
175
-
176
  def add_image(self, image_path):
177
  self.add_page()
178
  self.image(image_path, x=10, y=10, w=270)
179
 
180
  pdf = PDF(orientation='L', unit='mm', format='A4')
181
  pdf.add_page()
182
- pdf.add_table(dataframe)
183
 
184
  # Gerar gráficos e adicionar ao PDF
185
  def add_bar_labels(bars):
@@ -228,7 +208,26 @@ def generate_pdf_report(dataframe, media_tempo_medio_turma, output_pdf_path):
228
  plt.savefig(graph_path)
229
  pdf.add_image(graph_path)
230
 
231
- pdf.output(output_pdf_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232
 
233
  def processar_relatorio(html_file, tarefa_files):
234
  input_directory = "temp_files" # Diretório temporário para os arquivos
@@ -269,10 +268,11 @@ def processar_relatorio(html_file, tarefa_files):
269
  df.to_html(html_output_path, index=False)
270
 
271
  pdf_output_path = os.path.join(output_directory, "relatorio_final.pdf")
272
- generate_pdf_report(df, media_tempo_medio_turma, pdf_output_path)
273
 
274
  return df.to_html(index=False), html_output_path, pdf_output_path
275
 
 
276
  # Tema personalizado
277
  theme = gr.themes.Default(
278
  primary_hue="blue", # Cor principal (tons de azul)
 
137
  contador_df.to_csv(relatorio_csv_path, index=False)
138
  return contador_df, media_tempo_medio_turma
139
 
140
+ import pdfkit
141
+ from fpdf import FPDF
142
+ import matplotlib.pyplot as plt
143
+ import os
144
+
145
+ def generate_pdf_report(dataframe, media_tempo_medio_turma, html_path, output_pdf_path):
146
  class PDF(FPDF):
147
  def header(self):
148
  self.set_font('Arial', 'B', 12)
 
154
  self.set_font('Arial', 'I', 8)
155
  self.cell(0, 10, f'Page {self.page_no()}', 0, 0, 'C')
156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
  def add_image(self, image_path):
158
  self.add_page()
159
  self.image(image_path, x=10, y=10, w=270)
160
 
161
  pdf = PDF(orientation='L', unit='mm', format='A4')
162
  pdf.add_page()
 
163
 
164
  # Gerar gráficos e adicionar ao PDF
165
  def add_bar_labels(bars):
 
208
  plt.savefig(graph_path)
209
  pdf.add_image(graph_path)
210
 
211
+ # Salvar o PDF com os gráficos
212
+ temp_graphics_pdf = 'temp_graphics.pdf'
213
+ pdf.output(temp_graphics_pdf)
214
+
215
+ # Converter o HTML para PDF
216
+ temp_html_pdf = 'temp_html.pdf'
217
+ pdfkit.from_file(html_path, temp_html_pdf)
218
+
219
+ # Combinar os PDFs
220
+ from PyPDF2 import PdfMerger
221
+
222
+ merger = PdfMerger()
223
+ merger.append(temp_html_pdf)
224
+ merger.append(temp_graphics_pdf)
225
+ merger.write(output_pdf_path)
226
+ merger.close()
227
+
228
+ # Remover arquivos temporários
229
+ os.remove(temp_graphics_pdf)
230
+ os.remove(temp_html_pdf)
231
 
232
  def processar_relatorio(html_file, tarefa_files):
233
  input_directory = "temp_files" # Diretório temporário para os arquivos
 
268
  df.to_html(html_output_path, index=False)
269
 
270
  pdf_output_path = os.path.join(output_directory, "relatorio_final.pdf")
271
+ generate_pdf_report(df, media_tempo_medio_turma, html_output_path, pdf_output_path)
272
 
273
  return df.to_html(index=False), html_output_path, pdf_output_path
274
 
275
+
276
  # Tema personalizado
277
  theme = gr.themes.Default(
278
  primary_hue="blue", # Cor principal (tons de azul)