Files
bigdata/extract_table.py
T

91 lines
3.9 KiB
Python

import json
import os
import pdfplumber
import re
def extract_from_pdf(pdf_path, output_json_paths):
questions = []
current_q = None
global_id = 1
with pdfplumber.open(pdf_path) as pdf:
for page_num, page in enumerate(pdf.pages):
text = page.extract_text() or ""
# 当遇到综合分析题或页码超过142页时停止抽取
if "大数据工程技术人员技能竞赛综合分析题" in text or page_num >= 142:
print(f"在第 {page_num + 1} 页检测到综合分析题或达到客观题边界,停止解析。")
break
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
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 ""
# 拆分选项
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,
"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"共提取题目 {len(questions)} 道。")
single_count = sum(1 for q in questions if q['type'] == '单选')
multi_count = sum(1 for q in questions if q['type'] == '多选')
print(f"其中单选 {single_count} 道,多选 {multi_count} 道。")
for out_path in output_json_paths:
os.makedirs(os.path.dirname(os.path.abspath(out_path)), exist_ok=True)
with open(out_path, 'w', encoding='utf-8') as f:
json.dump(questions, f, ensure_ascii=False, indent=2)
print(f"已保存题库至:{out_path}")
if __name__ == "__main__":
targets = [
"d:/code/bigdata/questions.json",
"d:/code/bigdata/quiz-app/src/questions.json"
]
extract_from_pdf("d:/code/bigdata/题库.pdf", targets)