Add enhanced PDF parsing with Student fields and folder organization

This commit is contained in:
nmemmert committed 2025-11-06 10:29:56 -05:00
1 parent f265f160cb
commit 87b2b9ad4c
2 files changed
+71 -26

No files matched your search

+49 -19
View File
@@ -52,10 +52,21 @@ def extract_names_from_pdf(pdf_path):
try:
reader = PdfReader(pdf_path)
fields = reader.get_fields()
last_name = fields.get('Last Name', {}).get('/V', '')
first_name = fields.get('First Name', {}).get('/V', '')
if last_name and first_name:
return f"{last_name}_{first_name}.pdf"
# Extract all required fields
last_name = fields.get('Last Name', {}).get('/V', '').strip()
first_name = fields.get('First Name', {}).get('/V', '').strip()
grade = fields.get('Student Grade', {}).get('/V', '').strip()
school = fields.get('Student School', {}).get('/V', '').strip()
if last_name and first_name and grade and school:
return {
'filename': f"{last_name}_{first_name}.pdf",
'last_name': last_name,
'first_name': first_name,
'grade': grade,
'school': school
}
else:
return None
except Exception as e:
@@ -66,6 +77,10 @@ def process_job(job_id, files):
processed_files = []
temp_extract_dirs = []
# Create base Episode directory for this job
episode_dir = os.path.join(app.config['UPLOAD_FOLDER'], f'Episode_{job_id}')
os.makedirs(episode_dir, exist_ok=True)
for file_info in files:
filepath = file_info['path']
filename = file_info['name']
@@ -82,40 +97,55 @@ def process_job(job_id, files):
for f in files_in_zip:
if f.lower().endswith('.pdf'):
pdf_path = os.path.join(root, f)
new_name = extract_names_from_pdf(pdf_path)
if new_name:
new_path = os.path.join(root, new_name)
pdf_info = extract_names_from_pdf(pdf_path)
if pdf_info:
# Create folder structure: Episode/School/Grade/
school_dir = os.path.join(episode_dir, pdf_info['school'])
grade_dir = os.path.join(school_dir, pdf_info['grade'])
os.makedirs(grade_dir, exist_ok=True)
# Move file to appropriate folder with new name
new_path = os.path.join(grade_dir, pdf_info['filename'])
counter = 1
while os.path.exists(new_path):
base, ext = os.path.splitext(new_name)
new_path = os.path.join(root, f"{base}_{counter}{ext}")
base, ext = os.path.splitext(pdf_info['filename'])
new_path = os.path.join(grade_dir, f"{base}_{counter}{ext}")
counter += 1
os.rename(pdf_path, new_path)
processed_files.append(('success', os.path.relpath(new_path, extract_dir), new_path))
processed_files.append(('success', os.path.relpath(new_path, episode_dir), new_path))
else:
processed_files.append(('skipped', f, None))
else:
# Single PDF
new_name = extract_names_from_pdf(filepath)
if new_name:
new_filepath = os.path.join(app.config['UPLOAD_FOLDER'], f"{job_id}_{new_name}")
pdf_info = extract_names_from_pdf(filepath)
if pdf_info:
# Create folder structure: Episode/School/Grade/
school_dir = os.path.join(episode_dir, pdf_info['school'])
grade_dir = os.path.join(school_dir, pdf_info['grade'])
os.makedirs(grade_dir, exist_ok=True)
# Move file to appropriate folder with new name
new_filepath = os.path.join(grade_dir, pdf_info['filename'])
counter = 1
while os.path.exists(new_filepath):
base, ext = os.path.splitext(new_name)
new_filepath = os.path.join(app.config['UPLOAD_FOLDER'], f"{job_id}_{base}_{counter}{ext}")
base, ext = os.path.splitext(pdf_info['filename'])
new_filepath = os.path.join(grade_dir, f"{base}_{counter}{ext}")
counter += 1
os.rename(filepath, new_filepath)
processed_files.append(('success', os.path.basename(new_filepath), new_filepath))
processed_files.append(('success', os.path.relpath(new_filepath, episode_dir), new_filepath))
else:
processed_files.append(('skipped', filename, None))
# Create ZIP of all successful renames
# Create ZIP of the entire Episode directory structure
successful_files = [p for p in processed_files if p[0] == 'success']
if successful_files:
zip_output = os.path.join(app.config['UPLOAD_FOLDER'], f'renamed_{job_id}.zip')
with zipfile.ZipFile(zip_output, 'w') as zip_out:
for _, rel_name, full_path in successful_files:
zip_out.write(full_path, rel_name)
for root, dirs, files_in_dir in os.walk(episode_dir):
for file in files_in_dir:
file_path = os.path.join(root, file)
arcname = os.path.relpath(file_path, episode_dir)
zip_out.write(file_path, arcname)
job_queue[job_id]['zip_path'] = zip_output
job_queue[job_id]['zip_name'] = f'renamed_{job_id}.zip'