【发布时间】:2023-01-08 04:13:39
【问题描述】:
我正在尝试绘制从 fitz 使用 reportlab 检测到的文本块框
这是我尝试过的:
doc = fitz.open("demo.pdf")
canvas = Canvas("demo_.pdf", bottomup = True)
def draw_auto_fit_text_block(canvas, x_1, y_1, text_block_width, text_block_height, font_name, font_size, text_content):
text_block_frame = Frame(x_1, y_1, text_block_width, text_block_height, topPadding = 0, leftPadding = 0, rightPadding = 0, bottomPadding = 0, showBoundary = 1)
text_block_styles = ParagraphStyle(name = "Normal", fontName = font_name, fontSize = font_size)
text_block_content = text_content.replace('\n','<br />\n')
text_block_story = [Paragraph(text_block_content, style = text_block_styles)]
text_block_story_inframe = KeepInFrame(text_block_width, text_block_height, text_block_story)
text_block_frame.addFromList([text_block_story_inframe], canvas)
for page in doc:
page_width = page.rect.width
page_height = page.rect.height
print("[page width]", page_width)
print("[page height]", page_height)
canvas.setPageSize((page_width, page_height))
blocks = page.get_text("blocks")
for block in blocks:
block_content = block[4].replace("\n", " ").replace("- ", "-").strip()
block_x_0 = block[0]
block_y_0 = block[1]
block_x_1 = block[2]
block_y_1 = block[3]
block_width = block_x_1 - block_x_0
block_height = block_y_1 - block_y_0
block_y_0 = page_height - block_y_0
block_y_1 = page_height - block_y_0
draw_auto_fit_text_block(canvas, block_x_0, block_y_0, block_width, block_height, font_name = "NimbusRomNo9L-Regu", font_size = 9.0, text_content = block_content)
canvas.showPage()
canvas.save()
使用此代码,我无法使用正确的框坐标绘制文本。 任何人都可以提供帮助。
原始pdf是:
【问题讨论】:
标签: python coordinates reportlab