Files
bigdata/extract_table.py
T

77 lines
3.3 KiB
Python

import json
import pdfplumber
import re
def extract_from_pdf(pdf_path, output_json_path):
questions = []
current_q = None
global_id = 1
with pdfplumber.open(pdf_path) as pdf:
for page_num, page in enumerate(pdf.pages):
tables = page.extract_tables()
for table in tables:
for row in table:
if not row or len(row) < 5:
continue
col0 = str(row[0]).strip() if row[0] is not None else ""
if col0 in ["序号", "序\n号", "序", "号", "题目", "可选项"]:
continue
is_new_q = False
if col0.isdigit():
is_new_q = True
# q_id = int(col0)
title = row[1] if row[1] else ""
options_raw = row[2] if row[2] else ""
q_type = row[3] if row[3] else ""
q_answer = row[4] if row[4] else ""
source_file = row[5] if len(row) > 5 and row[5] else ""
# Split options
opts = re.split(r'(?=[A-H][\.、])', options_raw)
parsed_options = [opt.strip().replace('\n', '') for opt in opts if opt.strip()]
if is_new_q:
if current_q:
current_q["id"] = global_id
questions.append(current_q)
global_id += 1
current_q = {
"title": title.replace('\n', ''),
"options": parsed_options,
"type": q_type.replace('\n', '').strip(),
"answer": q_answer.replace('\n', '').strip(),
"source_id": col0, # keeping original id for reference
"page_number": page_num + 1,
"source_file": source_file.replace('\n', '').strip()
}
elif current_q:
if title:
current_q["title"] += title.replace('\n', '')
if parsed_options:
current_q["options"].extend(parsed_options)
if q_type:
current_q["type"] += q_type.replace('\n', '').strip()
if q_answer:
current_q["answer"] += q_answer.replace('\n', '').strip()
if source_file:
current_q["source_file"] += source_file.replace('\n', '').strip()
if current_q:
current_q["id"] = global_id
questions.append(current_q)
print(f"Extracted {len(questions)} questions via table parsing.")
bad = [x for x in questions if len(x['options']) < 2]
print(f"Questions with <2 options: {len(bad)}")
with open(output_json_path, 'w', encoding='utf-8') as f:
json.dump(questions, f, ensure_ascii=False, indent=2)
if __name__ == "__main__":
extract_from_pdf("d:/code/bigdata/题库.pdf", "d:/code/bigdata/questions.json")