程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

根据坐标在 PDF 中切片图像

发布于2023-02-03 21:22     阅读(802)     评论(0)     点赞(8)     收藏(2)


我有一个pdf,其中的问题要单独切片。我尝试了不同程度的成功的各种方法。现在我使用的方法是,我在上面添加了一个 OCR 层,并找出坐标 101. , 102. 直到 200

我使用的代码和示例输出

from PyPDF2 import PdfReader
import re

# Open the PDF file
with open('document.pdf', 'rb') as file:
    pdf = PdfReader(file)
    
    # Iterate through each page of the PDF
    for page_num in range(len(pdf.pages)):
        page = pdf.pages[page_num]
        text = page.extract_text()
        matches = re.finditer(r'(1\d{2}\.|2\d{2}\. )', text)
        print("Page number: ",page_num+1) # added this line
        for match in matches:
            print(f"Number {match.group()} found at coordinates x1:{page.mediabox[0] + match.start()}, y1:{page.mediabox[1]}, x2:{page.mediabox[0] + match.end()}, y2:{page.mediabox[3]}")

输出

Page number:  1
Number 101. found at coordinates x1:185, y1:0, x2:189, y2:777.6
Number 102. found at coordinates x1:494, y1:0, x2:498, y2:777.6
Page number:  2
Number 103. found at coordinates x1:0, y1:0, x2:4, y2:777.6
Number 104. found at coordinates x1:144, y1:0, x2:148, y2:777.6
Page number:  3
Number 105. found at coordinates x1:0, y1:0, x2:4, y2:778.32
Number 106. found at coordinates x1:329, y1:0, x2:333, y2:778.32

我的目标是将 pdf 切片成图像,将顶点从页面顶部到 101. 然后 101. 到 102. 然后 102. 到页面末尾。无法继续进行。却无法进行下一步。


解决方案


暂无回答



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:https://www.pythonheidong.com/blog/article/1895307/fd088ad3ef59729e375b/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

8 0
收藏该文
已收藏

评论内容:(最多支持255个字符)