PDFminer 从每页的标题中获取字体大小(迭代)

时间:2021-08-01 22:39:52

标签: python python-3.x pdf pdfminer

我对 python 和 PDFminer 很陌生,这对我来说有点复杂,我想要实现的是从 pdf 文件或幻灯片中提取每个页面的标题。

我的方法是获取每页文本行和字体大小的列表,然后我会选择最大的数字,因为幻灯片标题通常以较大的字体大小书写。

这是我目前所做的:

假设我想从这个 pdf 文件中获取第 8 页的标题。 File sample

这是第 8 页内容的样子:

enter image description here

这是获取每行所有页面字体大小的代码:

from pdfminer.high_level import extract_pages
from pdfminer.layout import LTTextContainer, LTChar,LTLine,LAParams
import os
path=r'cov.pdf'

Extract_Data=[]

for page_layout in extract_pages(path):
    for element in page_layout:
        if isinstance(element, LTTextContainer):
            for text_line in element:
                for character in text_line:
                    if isinstance(character, LTChar):
                        Font_size=character.size
            Extract_Data.append([Font_size,(element.get_text())])

生成的列表Extract_Data适用于pdf文档的所有页面。我的问题是如何为文档的每个页面(迭代)获取此列表?

仅对第 8 页的预期输出等等/然后如果我想选择页面标题,它将是字体大小值最高的项目(行):

[[32.039999999999964, 'Pandemic declaration \n'],
 [24.0, ' \n'],
 [24.0, ' \n'],
 [24.0,
  '•  On March 11, 2020, the World Health Organization \n(WHO) characterized COVID-19 as a pandemic. \n \n•  It has caused severe illness and death. It features \n \nsustained person-to-person spread worldwide. \n'],
 [24.0, ' \n'],
 [24.0, ' \n'],
 [24.0, ' \n'],
 [24.0, ' \n'],
 [24.0, '•  It poses an especially high risk for the elderly (60 or \n \n'],
 [24.0, ' \n'],
 [24.0, ' \n'],
 [24.0, ' \n'],
 [24.0, ' \n'],
 [24.0, ' \n'],
 [24.0,
  'older), people with preexisting health conditions such \nas high blood pressure, heart disease, lung disease, \n  \ndiabetes, autoimmune disorders, and certain workers. \n \n'],
 [24.0, ' \n'],
 [24.0, ' \n'],
 [24.0, ' \n'],
 [24.0, ' \n'],
 [24.0, ' \n'],
 [14.04, '8 \n']]

0 个答案:

没有答案