histlearn commited on
Commit
75c163e
·
verified ·
1 Parent(s): b0f7407

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -1
app.py CHANGED
@@ -71,6 +71,75 @@ class PDFReport(FPDF):
71
  'Este relatório é uma análise automática e deve ser validado junto à secretaria da escola.',
72
  0, new_x=XPos.LMARGIN, new_y=YPos.NEXT, align='C')
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  # Funções de plotagem
75
  def plotar_evolucao_bimestres(disciplinas_dados: List[Dict], temp_dir: str,
76
  titulo: Optional[str] = None,
@@ -193,7 +262,7 @@ def gerar_relatorio_pdf(df: pd.DataFrame, disciplinas_dados: List[Dict],
193
  0, new_x=XPos.LMARGIN, new_y=YPos.NEXT, align='L')
194
  pdf.ln(10)
195
 
196
- # Pontos de atenção
197
  pdf.set_font('Helvetica', 'B', 12)
198
  pdf.cell(0, 10, 'Pontos de Atenção:',
199
  0, new_x=XPos.LMARGIN, new_y=YPos.NEXT, align='L')
 
71
  'Este relatório é uma análise automática e deve ser validado junto à secretaria da escola.',
72
  0, new_x=XPos.LMARGIN, new_y=YPos.NEXT, align='C')
73
 
74
+ # Função de extração de tabelas do PDF
75
+ def extrair_tabelas_pdf(pdf_path: str) -> pd.DataFrame:
76
+ """Extrai tabelas do PDF usando stream para o nome e lattice para notas."""
77
+ try:
78
+ # Extrair nome do aluno usando stream
79
+ tables_header = camelot.read_pdf(
80
+ pdf_path,
81
+ pages='1',
82
+ flavor='stream',
83
+ edge_tol=500
84
+ )
85
+
86
+ info_aluno = {}
87
+
88
+ # Procurar nome do aluno
89
+ for table in tables_header:
90
+ df = table.df
91
+ for i in range(len(df)):
92
+ for j in range(len(df.columns)):
93
+ texto = str(df.iloc[i,j]).strip()
94
+ if 'Nome do Aluno' in texto:
95
+ try:
96
+ if j + 1 < len(df.columns):
97
+ nome = str(df.iloc[i,j+1]).strip()
98
+ elif i + 1 < len(df):
99
+ nome = str(df.iloc[i+1,j]).strip()
100
+ if nome and nome != 'Nome do Aluno:':
101
+ info_aluno['nome'] = nome
102
+ break
103
+ except:
104
+ continue
105
+
106
+ # Extrair tabela de notas usando lattice
107
+ tables_notas = camelot.read_pdf(
108
+ pdf_path,
109
+ pages='all',
110
+ flavor='lattice'
111
+ )
112
+
113
+ # Encontrar tabela de notas
114
+ df_notas = None
115
+ max_rows = 0
116
+
117
+ for table in tables_notas:
118
+ df_temp = table.df
119
+ if len(df_temp) > max_rows and 'Disciplina' in str(df_temp.iloc[0,0]):
120
+ max_rows = len(df_temp)
121
+ df_notas = df_temp.copy()
122
+ df_notas = df_notas.rename(columns={
123
+ 0: 'Disciplina',
124
+ 1: 'Nota B1', 2: 'Freq B1', 3: '%Freq B1', 4: 'AC B1',
125
+ 5: 'Nota B2', 6: 'Freq B2', 7: '%Freq B2', 8: 'AC B2',
126
+ 9: 'Nota B3', 10: 'Freq B3', 11: '%Freq B3', 12: 'AC B3',
127
+ 13: 'Nota B4', 14: 'Freq B4', 15: '%Freq B4', 16: 'AC B4',
128
+ 17: 'CF', 18: 'Nota Final', 19: 'Freq Final', 20: 'AC Final'
129
+ })
130
+
131
+ if df_notas is None:
132
+ raise ValueError("Tabela de notas não encontrada")
133
+
134
+ # Adicionar informações do aluno ao DataFrame
135
+ df_notas.attrs['nome'] = info_aluno.get('nome', 'Nome não encontrado')
136
+
137
+ return df_notas
138
+
139
+ except Exception as e:
140
+ logger.error(f"Erro na extração das tabelas: {str(e)}")
141
+ raise
142
+
143
  # Funções de plotagem
144
  def plotar_evolucao_bimestres(disciplinas_dados: List[Dict], temp_dir: str,
145
  titulo: Optional[str] = None,
 
262
  0, new_x=XPos.LMARGIN, new_y=YPos.NEXT, align='L')
263
  pdf.ln(10)
264
 
265
+ # Pontos de atenção
266
  pdf.set_font('Helvetica', 'B', 12)
267
  pdf.cell(0, 10, 'Pontos de Atenção:',
268
  0, new_x=XPos.LMARGIN, new_y=YPos.NEXT, align='L')