python合并pdf 加书签,使用PyPDF2添加书签

The documentation for PyPDF2 states that it's possible to add nested bookmarks to PDF files, and the code appears (upon reading) to support this.

Adding a bookmark to the root tree is easy (see code below), but I can't figure out what I need to pass as the parent argument to create a nested bookmark. I want to create a structure something like this:

Group A

Page 1

Page 2

Group A

Page 3

Page 4

Is this possible?

Sample code to add a bookmark to the root of the tree:

#!/usr/bin/env python

from PyPDF2 import PdfFileWriter, PdfFileReader

output = PdfFileWriter() # open output

input = PdfFileReader(open('input.pdf', 'rb')) # open input

output.addPage(input.getPage(0)) # insert page

output.addBookmark('Hello, World', 0, parent=None) # add bookmark

解决方案

The addBookmark method returns a reference to the bookmark it created, which can be used as the parent to another bookmark. e.g.

#!/usr/bin/env python

from PyPDF2 import PdfFileWriter, PdfFileReader

output = PdfFileWriter()

input1 = PdfFileReader(open('introduction.pdf', 'rb'))

output.addPage(input1.getPage(0))

input2 = PdfFileReader(open('hello.pdf', 'rb'))

output.addPage(input2.getPage(0))

parent = output.addBookmark('Introduction', 0) # add parent bookmark

output.addBookmark('Hello, World', 0, parent) # add child bookmark

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值