【问题标题】:PyPDF2 : extract table of contents/outlines and their page numberPyPDF2:提取目录/大纲及其页码
【发布时间】:2021-09-25 04:05:55
【问题描述】:

我正在尝试使用 Python (PyPDF2) 从 PDF 及其页码中提取目录/大纲,我知道 reader.outlines 但它没有返回正确的页码。

PDF 示例:https://www.annualreports.com/HostedData/AnnualReportArchive/l/NASDAQ_LOGM_2018.pdf

reader.outlines 的输出是:

[{'/Title': '2018 Highlights', '/Page': IndirectObject(5, 0), '/Type': '/Fit'},
{'/Title': 'Letter to Stockholders', '/Page': IndirectObject(6, 0), '/Type': '/Fit'}, 
...
{'/Title': 'Part I', '/Page': IndirectObject(10, 0), '/Type': '/Fit'}, 
[{'/Title': 'Item 1. Business', '/Page': IndirectObject(10, 0), '/Type': '/Fit'}, 
{'/Title': 'Item 1A. Risk Factors', '/Page': IndirectObject(19, 0), '/Type': '/Fit'}
...

例如,PART I 不应该从第 10 页开始,我是否遗漏了什么? 有人有替代品吗?

我尝试过 PyMupdf、Tabula 和 getDestinationPageNumber 方法,但没有成功。

提前谢谢你。

【问题讨论】:

  • @KJ 我刚刚使用 PdfFileReader(来自 PyPDF2)阅读了 pdf 并打印了大纲,这就是我觉得奇怪的原因。

标签: python pdf pypdf2 tableofcontents


【解决方案1】:

Martin Thoma's answer 正是我所需要的 (PyMuPDF)。 Diblo Dk's answer 也是一个有趣的解决方法 (PyPDF2)。

我引用的正是 Martin Thoma 的代码:

from typing import Dict

import fitz  # pip install pymupdf


def get_bookmarks(filepath: str) -> Dict[int, str]:
    # WARNING! One page can have multiple bookmarks!
    bookmarks = {}
    with fitz.open(filepath) as doc:
        toc = doc.getToC()  # [[lvl, title, page, …], …]
        for level, title, page in toc:
            bookmarks[page] = title
    return bookmarks


print(get_bookmarks("my.pdf"))

【讨论】:

    【解决方案2】:

    查看名为 Tabula 的包。使用这个包提取表真的很容易。该软件包还具有使您能够从跨越多个页面的表格中提取内容的选项。

    这里是值得一试的链接:- https://towardsdatascience.com/scraping-table-data-from-pdf-files-using-a-single-line-in-python-8607880c750

    【讨论】:

    • 我刚刚尝试了 tabula,但它什么也没返回(= 空列表)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-07
    • 2018-03-28
    • 1970-01-01
    • 2011-01-19
    • 2010-11-04
    相关资源
    最近更新 更多