使用Python給PDF添加目錄書(shū)簽的實(shí)現(xiàn)方法
0、庫(kù)的選擇——pypdf
Python | 3.11 | 3.10 | 3.9 | 3.8 | 3.7 | 3.6 | 2.7 |
|---|---|---|---|---|---|---|---|
pypdf>=3.0 | YES | YES | YES | YES | YES | YES | |
PyPDF2>=2.0 | YES | YES | YES | YES | YES | YES | |
PyPDF2 1.20.0 - 1.28.4 | YES | YES | YES | YES | YES | YES | |
PyPDF2 1.15.0 - 1.20.0 | YES |
我的版本
Python=3.6.13
pypdf=3.16.2
1、添加書(shū)簽——方法add_outline_item的使用
# https://zhuanlan.zhihu.com/p/603340639
import pypdf #
import sys
wk_in_file_name = 'PythonTutorial.pdf'
input1 = open(wk_in_file_name, "rb") # 打開(kāi)需要添加書(shū)簽的PDF
writer = pypdf.PdfWriter() # 創(chuàng)建一個(gè)PdfWriter類
writer.append(input1) # 將PDF讀入writer中,然后進(jìn)行書(shū)簽的編輯
writer.add_outline_item(title='10', page_number=10, parent=None) # 添加第一個(gè)書(shū)簽
writer.add_outline_item(title='11', page_number=11, parent=None) # 添加第二個(gè)書(shū)簽
# Write to an output PDF document
output = open('01_' + wk_in_file_name, "wb") # 如果wk_out_file_name不存在,則創(chuàng)建一個(gè)
writer.write(output) # 將添加書(shū)簽后的PDF保存
# Close File Descriptors
writer.close()
output.close()
print('pypdf.__version__=', pypdf.__version__)
print('sys.version=', sys.version)
pass運(yùn)行結(jié)果

2、添加子書(shū)簽——參數(shù)parent的使用
# https://zhuanlan.zhihu.com/p/603340639
import pypdf
wk_in_file_name = 'PythonTutorial.pdf'
writer = pypdf.PdfWriter()
input1 = open(wk_in_file_name, "rb")
writer.append(input1)
parent_bookmark_0 = writer.add_outline_item(title='10', page_number=10, parent=None) # 添加第一個(gè)書(shū)簽
writer.add_outline_item(title='10_1', page_number=11, parent=parent_bookmark_0) # 添加第一個(gè)書(shū)簽的子書(shū)簽
parent_bookmark_1 = writer.add_outline_item(title='11', page_number=20, parent=None) # 添加第二個(gè)書(shū)簽
writer.add_outline_item(title='11_1', page_number=21, parent=parent_bookmark_1) # 添加第二個(gè)書(shū)簽的子書(shū)簽
# Write to an output PDF document
output = open('02_'+wk_in_file_name, "wb")
writer.write(output)
# Close File Descriptors
writer.close()
output.close()
pass運(yùn)行結(jié)果

3、讀取txt文件
# https://blog.csdn.net/kobeyu652453/article/details/106876829
f = open('dir.txt', 'r', encoding='utf8')
# f = open('dir.txt', encoding='gbk', errors='ignore'), errors='ignore'
# f = open('dir.txt', encoding='gb18030', errors='ignore')
line1 = f.readline() # 讀取第一行,大文件readline
# https://blog.csdn.net/andyleo0111/article/details/87878784
lines = f.readlines() # 讀取所有行,小文件readlines
num_lines = len(lines) # 標(biāo)題的總個(gè)數(shù)
txt = []
for line in lines:
txt.append(line.strip())
print(line.strip())
line.strip() # 去掉末尾的'\n'
line.split(' ') # 根據(jù)line中' '進(jìn)行分割
line.count('.') # 有n個(gè)'.'就是n+1級(jí)標(biāo)題
print(txt)
f.close() # 關(guān)閉文件
print('f.closed=', f.closed)運(yùn)行結(jié)果
D:\SoftProgram\JetBrains\anaconda3_202303\envs\py3_6_for_TimeSeries\python.exe E:\program\python\gitTemp\pdf\test\03_read_txt.py 1 課前甜點(diǎn) 3 2 使用Python解釋器 5 2.1 調(diào)用解釋器 5 2.1.1 傳入?yún)?shù) 6 2.1.2 交互模式 6 2.2 解釋器的運(yùn)行環(huán)境 6 2.2.1 源文件的字符編碼 6 3 Python的非正式介紹 9 3.1 Python作為計(jì)算器使用 9 3.1.1 數(shù)字 9 3.1.2 字符串 11 3.1.3 列表 14 3.2 走向編程的第一步 15 4 其他流程控制工具 17 4.1 if語(yǔ)句 17 4.2 for語(yǔ)句 17 4.3 range()函數(shù) 18 4.4 break和continue語(yǔ)句,以及循環(huán)中的else子句 19 4.5 pass 語(yǔ)句 20 4.6 定義函數(shù) 20 4.7 函數(shù)定義的更多形式 22 4.8 小插曲:編碼風(fēng)格 29 ['1 課前甜點(diǎn) 3', '2 使用Python解釋器 5', '2.1 調(diào)用解釋器 5', '2.1.1 傳入?yún)?shù) 6', '2.1.2 交互模式 6', '2.2 解釋器的運(yùn)行環(huán)境 6', '2.2.1 源文件的字符編碼 6', '3 Python的非正式介紹 9', '3.1 Python作為計(jì)算器使用 9', '3.1.1 數(shù)字 9', '3.1.2 字符串 11', '3.1.3 列表 14', '3.2 走向編程的第一步 15', '4 其他流程控制工具 17', '4.1 if語(yǔ)句 17', '4.2 for語(yǔ)句 17', '4.3 range()函數(shù) 18', '4.4 break和continue語(yǔ)句,以及循環(huán)中的else子句 19', '4.5 pass 語(yǔ)句 20', '4.6 定義函數(shù) 20', '4.7 函數(shù)定義的更多形式 22', '4.8 小插曲:編碼風(fēng)格 29'] f.closed= True 進(jìn)程已結(jié)束,退出代碼0
4、從txt中讀取目錄與頁(yè)碼并寫(xiě)入PDF的書(shū)簽
# https://blog.csdn.net/kobeyu652453/article/details/106876829
import pypdf
wk_in_file_name = 'PythonTutorial.pdf'
writer = pypdf.PdfWriter()
input1 = open(wk_in_file_name, "rb")
writer.append(input1)
f = open('dir.txt', 'r', encoding='utf8')
lines = f.readlines() # 讀取所有行
num_lines = len(lines) # 標(biāo)題的總個(gè)數(shù)
txt = []
for line in lines:
line = line.strip() # 去掉末尾的'\n'
pline = line.split(' ') # 根據(jù)line中' '進(jìn)行分割
level = line.count('.') # 有n個(gè)'.'就是n+1級(jí)標(biāo)題
if level == 0:
bookmark_parent_0 = writer.add_outline_item(title=pline[0] + pline[1], page_number=int(pline[-1]), parent=None)
elif level == 1:
bookmark_parent_1 = writer.add_outline_item(title=pline[0] + pline[1], page_number=int(pline[-1]),
parent=bookmark_parent_0)
else:
writer.add_outline_item(title=pline[0] + pline[1], page_number=int(pline[-1]), parent=bookmark_parent_1)
# Write to an output PDF document
output = open('04_'+wk_in_file_name, "wb")
writer.write(output)
# Close File Descriptors
writer.close()
output.close()
f.close() # 關(guān)閉文件
print('f.closed=', f.closed)運(yùn)行結(jié)果

5、添加偏置
# https://blog.csdn.net/kobeyu652453/article/details/106876829
import pypdf
wk_in_file_name = 'PythonTutorial.pdf'
writer = pypdf.PdfWriter()
input1 = open(wk_in_file_name, "rb")
writer.append(input1)
f = open('dir.txt', 'r', encoding='utf8')
lines = f.readlines() # 讀取所有行
num_lines = len(lines) # 標(biāo)題的總個(gè)數(shù)
offset = 5 # 添加偏置
txt = []
bookmark_parent_0 = None
bookmark_parent_1 = None
for line in lines:
line = line.strip() # 去掉末尾的'\n'
pline = line.split(' ') # 根據(jù)line中' '進(jìn)行分割
level = line.count('.') # 有n個(gè)'.'就是n+1級(jí)標(biāo)題
page_title = pline[0] + ' ' + pline[1]
page_num = int(pline[-1]) + offset
if level == 0:
bookmark_parent_0 = writer.add_outline_item(title=page_title, page_number=page_num, parent=None)
elif level == 1:
bookmark_parent_1 = writer.add_outline_item(title=page_title, page_number=page_num, parent=bookmark_parent_0)
else:
writer.add_outline_item(title=page_title, page_number=page_num, parent=bookmark_parent_1)
print(line.strip())
print(txt)
# Write to an output PDF document
output = open('05_' + wk_in_file_name, "wb")
writer.write(output)
# Close File Descriptors
writer.close()
output.close()
f.close() # 關(guān)閉文件
print('f.closed=', f.closed)運(yùn)行結(jié)果:

6、dir中沒(méi)有頁(yè)碼的情況
# https://blog.csdn.net/kobeyu652453/article/details/106876829
import pypdf
wk_in_file_name = 'PythonTutorial.pdf'
writer = pypdf.PdfWriter()
input1 = open(wk_in_file_name, "rb")
writer.append(input1)
f = open('dir.txt', 'r', encoding='utf8')
lines = f.readlines() # 讀取所有行
num_lines = len(lines) # 標(biāo)題的總個(gè)數(shù)
offset = 5 # 添加偏置
txt = []
bookmark_parent_0 = None
bookmark_parent_1 = None
for line in lines:
line = line.strip() # 去掉末尾的'\n'
pline = line.split(' ') # 根據(jù)line中' '進(jìn)行分割
level = line.count('.') # 有n個(gè)'.'就是n+1級(jí)標(biāo)題
page_title = pline[0] + ' ' + pline[1]
page_num = offset
if level == 0:
bookmark_parent_0 = writer.add_outline_item(title=page_title, page_number=page_num, parent=None)
elif level == 1:
bookmark_parent_1 = writer.add_outline_item(title=page_title, page_number=page_num, parent=bookmark_parent_0)
else:
writer.add_outline_item(title=page_title, page_number=page_num, parent=bookmark_parent_1)
print(line.strip())
print(txt)
# Write to an output PDF document
output = open('06_' + wk_in_file_name, "wb")
writer.write(output)
# Close File Descriptors
writer.close()
output.close()
f.close() # 關(guān)閉文件
print('f.closed=', f.closed)運(yùn)行結(jié)果

以上就是使用Python給PDF添加目錄書(shū)簽的實(shí)現(xiàn)方法的詳細(xì)內(nèi)容,更多關(guān)于Python給PDF添加目錄書(shū)簽的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Django入門優(yōu)缺點(diǎn)及環(huán)境搭建流程
這篇文章主要為大家介紹了Django入門優(yōu)缺點(diǎn)及環(huán)境搭建流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
Python中用Descriptor實(shí)現(xiàn)類級(jí)屬性(Property)詳解
這篇文章主要介紹了Python中用Descriptor實(shí)現(xiàn)類級(jí)屬性(Property)詳解,本文先是講解了decorator是什么,然后給出了通過(guò)Descriptor來(lái)做一個(gè)類級(jí)的Property實(shí)例,需要的朋友可以參考下2014-09-09
Python實(shí)現(xiàn)vlog生成器的示例代碼
vlog,全稱為Video?blog,意為影音博客,也有翻譯為微錄。本文將嘗試用Python基于Moviepy從一個(gè)文本文件中自動(dòng)生成一個(gè)視頻格式的vlog,感興趣的可以了解一下2023-01-01
Python數(shù)據(jù)結(jié)構(gòu)列表
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)列表,本文重點(diǎn)內(nèi)容主要是對(duì)列表數(shù)據(jù)結(jié)構(gòu)的使用,在Python中,序列是一組按順序排列的值。Python?有?3?種內(nèi)置的序列類型:字符串、?元組和列表,下面一起進(jìn)入文章了解更詳細(xì)內(nèi)容吧,需要的小伙伴可以參考一下</P><P>2021-12-12
如何解決requests,已經(jīng)安裝卻無(wú)法import問(wèn)題
這篇文章主要介紹了如何解決requests,已經(jīng)安裝卻無(wú)法import問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06

