结构图如下:
只要一直循环,就可以从Box -> Line -> Char 使用.bbox 属性就可以得到坐标,一共有4个值,分别表示:
- x0:从页面左侧到框左边缘的距离
- y0:从页面底部到框的下边缘的距离
- x1:从页面左侧到方框右边缘的距离
- y1:从页面底部到框的上边缘的距离
示例
import requests
import io
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdfpage import PDFPage
from pdfminer.pdfparser import PDFParser
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import PDFPageAggregator
from pdfminer.layout import LAParams, LTText, LTChar, LTAnno
def parse_char_layout(layout):
"""解析页面内容,一个字母一个字母的解析"""
for textbox in layout:
if isinstance(textbox, LTText):
for line in textbox:
for char in line:
if isinstance(char, LTAnno) or char.get_text() == ' ':
pass
elif isinstance(char, LTChar):
print("坐标 x:", char.bbox[0], "y:", char.bbox[3], " ||| ", char.get_text())
if __name__ == '__main__':
req = requests.get("http://www.africau.edu/images/default/sample.pdf")
fp = io.BytesIO(req.content)
parser = PDFParser(fp)
doc: PDFDocument = PDFDocument(parser)
rsrcmgr = PDFResourceManager()
laparams = LAParams()
device = PDFPageAggregator(rsrcmgr, laparams=laparams)
interpreter = PDFPageInterpreter(rsrcmgr, device)
interpreter = PDFPageInterpreter(rsrcmgr, device)
for page in PDFPage.create_pages(doc):
print('================ 新页面 ================')
interpreter.process_page(page)
layout = device.get_result()
parse_char_layout(layout)
|