Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -129,20 +129,32 @@ def extrair_tabelas_pdf(pdf_path):
|
|
129 |
if len(tables) == 0:
|
130 |
raise ValueError("Nenhuma tabela foi extraída do PDF.")
|
131 |
|
132 |
-
|
133 |
-
|
134 |
info_aluno = {}
|
135 |
-
for
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
df_temp = table.df
|
147 |
if any('Disciplina' in str(col) for col in df_temp.iloc[0]) or \
|
148 |
any('Bimestre' in str(col) for col in df_temp.iloc[0]):
|
@@ -157,12 +169,17 @@ def extrair_tabelas_pdf(pdf_path):
|
|
157 |
})
|
158 |
break
|
159 |
|
160 |
-
if df.empty:
|
161 |
-
raise ValueError("A tabela
|
162 |
|
163 |
-
|
164 |
-
|
|
|
|
|
|
|
|
|
165 |
|
|
|
166 |
return df
|
167 |
|
168 |
except Exception as e:
|
@@ -462,14 +479,31 @@ def gerar_relatorio_pdf(df, disciplinas_dados, grafico_basica, grafico_diversifi
|
|
462 |
|
463 |
pdf.set_font('Helvetica', '', 11)
|
464 |
if hasattr(df, 'attrs'):
|
465 |
-
|
466 |
-
|
467 |
-
|
468 |
-
|
469 |
-
|
470 |
-
|
471 |
-
|
472 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
473 |
|
474 |
pdf.ln(10)
|
475 |
|
|
|
129 |
if len(tables) == 0:
|
130 |
raise ValueError("Nenhuma tabela foi extraída do PDF.")
|
131 |
|
132 |
+
# Processar todas as tabelas procurando informações do aluno
|
|
|
133 |
info_aluno = {}
|
134 |
+
for table in tables:
|
135 |
+
df_temp = table.df
|
136 |
+
for i, row in df_temp.iterrows():
|
137 |
+
# Procurar por variações dos campos
|
138 |
+
if any(campo in str(row[0]).upper() for campo in ['NOME DO ALUNO:', 'NOME:', 'ALUNO:']):
|
139 |
+
nome = row[1].strip() if len(row) > 1 else ''
|
140 |
+
if nome and 'nome' not in info_aluno: # Evitar sobrescrever se já encontrou
|
141 |
+
info_aluno['nome'] = nome
|
142 |
+
elif any(campo in str(row[0]).upper() for campo in ['RA:', 'R.A.:', 'REGISTRO ACADÊMICO:']):
|
143 |
+
ra = row[1].strip() if len(row) > 1 else ''
|
144 |
+
if ra and 'ra' not in info_aluno: # Evitar sobrescrever se já encontrou
|
145 |
+
info_aluno['ra'] = ra
|
146 |
+
elif any(campo in str(row[0]).upper() for campo in ['ESCOLA:', 'UNIDADE:', 'UNIDADE ESCOLAR:']):
|
147 |
+
escola = row[1].strip() if len(row) > 1 else ''
|
148 |
+
if escola and 'escola' not in info_aluno:
|
149 |
+
info_aluno['escola'] = escola
|
150 |
+
elif any(campo in str(row[0]).upper() for campo in ['TURMA:', 'SÉRIE:', 'ANO/SÉRIE:']):
|
151 |
+
turma = row[1].strip() if len(row) > 1 else ''
|
152 |
+
if turma and 'turma' not in info_aluno:
|
153 |
+
info_aluno['turma'] = turma
|
154 |
+
|
155 |
+
# Encontrar a tabela com as notas
|
156 |
+
df = None
|
157 |
+
for table in tables:
|
158 |
df_temp = table.df
|
159 |
if any('Disciplina' in str(col) for col in df_temp.iloc[0]) or \
|
160 |
any('Bimestre' in str(col) for col in df_temp.iloc[0]):
|
|
|
169 |
})
|
170 |
break
|
171 |
|
172 |
+
if df is None or df.empty:
|
173 |
+
raise ValueError("A tabela de notas não foi encontrada.")
|
174 |
|
175 |
+
# Adicionar informações do aluno ao DataFrame
|
176 |
+
# Garantir valores padrão se não encontrados
|
177 |
+
df.attrs['nome'] = info_aluno.get('nome', 'Nome não encontrado')
|
178 |
+
df.attrs['ra'] = info_aluno.get('ra', 'RA não encontrado')
|
179 |
+
df.attrs['escola'] = info_aluno.get('escola', 'Escola não encontrada')
|
180 |
+
df.attrs['turma'] = info_aluno.get('turma', 'Turma não encontrada')
|
181 |
|
182 |
+
print("Informações do aluno encontradas:", info_aluno)
|
183 |
return df
|
184 |
|
185 |
except Exception as e:
|
|
|
479 |
|
480 |
pdf.set_font('Helvetica', '', 11)
|
481 |
if hasattr(df, 'attrs'):
|
482 |
+
nome = df.attrs.get('nome', 'Nome não encontrado')
|
483 |
+
ra = df.attrs.get('ra', 'RA não encontrado')
|
484 |
+
escola = df.attrs.get('escola', 'Escola não encontrada')
|
485 |
+
turma = df.attrs.get('turma', 'Turma não encontrada')
|
486 |
+
|
487 |
+
# Adicionar rótulos em negrito
|
488 |
+
pdf.set_font('Helvetica', 'B', 11)
|
489 |
+
pdf.cell(30, 7, 'Nome:', 0, 0)
|
490 |
+
pdf.set_font('Helvetica', '', 11)
|
491 |
+
pdf.cell(0, 7, nome, 0, new_x=XPos.LMARGIN, new_y=YPos.NEXT)
|
492 |
+
|
493 |
+
pdf.set_font('Helvetica', 'B', 11)
|
494 |
+
pdf.cell(30, 7, 'RA:', 0, 0)
|
495 |
+
pdf.set_font('Helvetica', '', 11)
|
496 |
+
pdf.cell(0, 7, ra, 0, new_x=XPos.LMARGIN, new_y=YPos.NEXT)
|
497 |
+
|
498 |
+
pdf.set_font('Helvetica', 'B', 11)
|
499 |
+
pdf.cell(30, 7, 'Escola:', 0, 0)
|
500 |
+
pdf.set_font('Helvetica', '', 11)
|
501 |
+
pdf.cell(0, 7, escola, 0, new_x=XPos.LMARGIN, new_y=YPos.NEXT)
|
502 |
+
|
503 |
+
pdf.set_font('Helvetica', 'B', 11)
|
504 |
+
pdf.cell(30, 7, 'Turma:', 0, 0)
|
505 |
+
pdf.set_font('Helvetica', '', 11)
|
506 |
+
pdf.cell(0, 7, turma, 0, new_x=XPos.LMARGIN, new_y=YPos.NEXT)
|
507 |
|
508 |
pdf.ln(10)
|
509 |
|