python搜索pdf内容所在页码_使用pyPDF从文档中检索页码

本文提供了一段Python代码示例,演示如何使用PyPDF2库从PDF文档中检索内容所在的页码。代码首先尝试探索PDF的PageLabels对象,然后解析页码类型和起始页码,并提供了处理十进制和罗马数字页码的方法。
摘要由CSDN通过智能技术生成

答案很好。但是,由于稍后(dreamer)请求了一个工作代码示例,而且我今天也遇到了同样的问题,所以我想添加一些注释。pdf结构并不统一;您可以依赖的东西很少,因此任何工作代码示例都不太可能适合每个人。一个很好的解释可以找到in this answer。

正如kindall所解释的,您很可能需要探索您正在处理的pdf文件。

就像这样:import sys

import PyPDF2 as pyPdf

"""Open your pdf"""

pdf = pyPdf.PdfFileReader(open(sys.argv[1], "rb"))

"""Explore the /PageLabels (if it exists)"""

try:

page_label_type = pdf.trailer["/Root"]["/PageLabels"]

print(page_label_type)

except:

print("No /PageLabel object")

"""Select the item that is most likely to contain the information you desire; e.g.

{'/Nums': [0, IndirectObject(42, 0)]}

here, we only have "/Num". """

try:

page_label_type = pdf.trailer["/Root"]["/PageLabels"]["/Nums"]

print(page_label_type)

except:

print("No /PageLabel object")

"""If you see a list, like

[0, IndirectObject(42, 0)]

get the correct item from it"""

try:

page_label_type = pdf.trailer["/Root"]["/PageLabels"]["/Nums"][1]

print(page_label_type)

except:

print("No /PageLabel object")

"""If you then have an indirect object, like

IndirectObject(42, 0)

use getObject()"""

try:

page_label_type = pdf.trailer["/Root"]["/PageLabels"]["/Nums"][1].getObject()

print(page_label_type)

except:

print("No /PageLabel object")

"""Now we have e.g.

{'/S': '/r', '/St': 21}

meaning roman numerals, starting with page 21, i.e. xxi. We can now also obtain the two variables directly."""

try:

page_label_type = pdf.trailer["/Root"]["/PageLabels"]["/Nums"][1].getObject()["/S"]

print(page_label_type)

start_page = pdf.trailer["/Root"]["/PageLabels"]["/Nums"][1].getObject()["/St"]

print(start_page)

except:

print("No /PageLabel object")从ISO pdf 1.7规范(相关章节here)中可以看到,如何标记页面有很多可能性。作为一个简单的工作示例,请考虑这个脚本,它将至少处理十进制(阿拉伯语)和罗马数字:

脚本:import sys

import PyPDF2 as pyPdf

def arabic_to_roman(arabic):

roman = ''

while arabic >= 1000:

roman += 'm'

arabic -= 1000

diffs = [900, 500, 400, 300, 200, 100, 90, 50, 40, 30, 20, 10, 9, 5, 4, 3, 2, 1]

digits = ['cm', 'd', 'cd', 'ccc', 'cc', 'c', 'xc', 'l', 'xl', 'xxx', 'xx', 'x', 'ix', 'v', 'iv', 'iii', 'ii', 'i']

for i in range(len(diffs)):

if arabic >= diffs[i]:

roman += digits[i]

arabic -= diffs[i]

return(roman)

def get_page_labels(pdf):

try:

page_label_type = pdf.trailer["/Root"]["/PageLabels"]["/Nums"][1].getObject()["/S"]

except:

page_label_type = "/D"

try:

page_start = pdf.trailer["/Root"]["/PageLabels"]["/Nums"][1].getObject()["/St"]

except:

page_start = 1

page_count = pdf.getNumPages()

##or, if you feel fancy, do:

#page_count = pdf.trailer["/Root"]["/Pages"]["/Count"]

page_stop = page_start + page_count

if page_label_type == "/D":

page_numbers = list(range(page_start, page_stop))

for i in range(len(page_numbers)):

page_numbers[i] = str(page_numbers[i])

elif page_label_type == '/r':

page_numbers_arabic = range(page_start, page_stop)

page_numbers = []

for i in range(len(page_numbers_arabic)):

page_numbers.append(arabic_to_roman(page_numbers_arabic[i]))

print(page_label_type)

print(page_start)

print(page_count)

print(page_numbers)

pdf = pyPdf.PdfFileReader(open(sys.argv[1], "rb"))

get_page_labels(pdf)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值