提问人:jonathanlin1112 提问时间:10/28/2023 最后编辑:jonathanlin1112 更新时间:10/28/2023 访问量:38
如何将所有多项选择题装在 PDF 中,以便用户可以选择、裁剪,然后合并到一个文件中?
How can I box all multiple-choice questions in a PDF so that they can be selected by the user, cropped, and then merged into a single file?
问:
我目前正在编写一个 Python 程序,该程序需要两个 PDF 文件、一张试卷和一张答卷。该程序最初将允许用户预览两篇论文。
我将使用的试卷链接:https://www.cambridgeinternational.org/Images/520512-june-2021-question-paper-21.pdf
用按钮确认他们选择的文件后,将打开一个新窗口,其中论文中的每个多项选择题都将单独装箱。我目前正在研究这个功能。
下面是我目前的尝试,我还在努力弄清楚整体概念
import tkinter as tk
from tkinter import filedialog, messagebox
from PyPDF2 import PdfReader
import re
def box_questions(pdf_path, canvas):
pdf = PdfReader(open(pdf_path, 'rb'))
num_pages = len(pdf.pages)
for page_num in range(num_pages):
page = pdf.pages[page_num]
page_text = page.extract_text()
questions = []
start_index = 0
for match in re.finditer(r'^\d+\.', page_text, re.MULTILINE):
end_index = match.end()
questions.append((start_index, end_index))
start_index = end_index
for start, end in questions:
x1 = 10
y1 = (start + end) // 2
x2 = canvas.winfo_width() - 10
y2 = y1 + 20
canvas.create_rectangle(x1, y1, x2, y2, outline="red", width=2)
root = tk.Tk()
root.title("PDF Questions Highlighter")
canvas = tk.Canvas(root, width=800, height=600)
canvas.pack()
pdf_path = filedialog.askopenfilename(title="Select PDF File")
if pdf_path:
box_questions(pdf_path, canvas)
else:
messagebox.showerror("Error", "Please select a PDF file.")
root.mainloop()
我将不胜感激向我提供的任何意见或建议。
答: 暂无答案
评论