使用Python給PDF添加目錄書簽的實現(xiàn)方法
0、庫的選擇——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、添加書簽——方法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") # 打開需要添加書簽的PDF
writer = pypdf.PdfWriter() # 創(chuàng)建一個PdfWriter類
writer.append(input1) # 將PDF讀入writer中,然后進行書簽的編輯
writer.add_outline_item(title='10', page_number=10, parent=None) # 添加第一個書簽
writer.add_outline_item(title='11', page_number=11, parent=None) # 添加第二個書簽
# Write to an output PDF document
output = open('01_' + wk_in_file_name, "wb") # 如果wk_out_file_name不存在,則創(chuàng)建一個
writer.write(output) # 將添加書簽后的PDF保存
# Close File Descriptors
writer.close()
output.close()
print('pypdf.__version__=', pypdf.__version__)
print('sys.version=', sys.version)
pass運行結(jié)果

2、添加子書簽——參數(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) # 添加第一個書簽
writer.add_outline_item(title='10_1', page_number=11, parent=parent_bookmark_0) # 添加第一個書簽的子書簽
parent_bookmark_1 = writer.add_outline_item(title='11', page_number=20, parent=None) # 添加第二個書簽
writer.add_outline_item(title='11_1', page_number=21, parent=parent_bookmark_1) # 添加第二個書簽的子書簽
# 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運行結(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) # 標題的總個數(shù)
txt = []
for line in lines:
txt.append(line.strip())
print(line.strip())
line.strip() # 去掉末尾的'\n'
line.split(' ') # 根據(jù)line中' '進行分割
line.count('.') # 有n個'.'就是n+1級標題
print(txt)
f.close() # 關(guān)閉文件
print('f.closed=', f.closed)運行結(jié)果
D:\SoftProgram\JetBrains\anaconda3_202303\envs\py3_6_for_TimeSeries\python.exe E:\program\python\gitTemp\pdf\test\03_read_txt.py 1 課前甜點 3 2 使用Python解釋器 5 2.1 調(diào)用解釋器 5 2.1.1 傳入?yún)?shù) 6 2.1.2 交互模式 6 2.2 解釋器的運行環(huán)境 6 2.2.1 源文件的字符編碼 6 3 Python的非正式介紹 9 3.1 Python作為計算器使用 9 3.1.1 數(shù)字 9 3.1.2 字符串 11 3.1.3 列表 14 3.2 走向編程的第一步 15 4 其他流程控制工具 17 4.1 if語句 17 4.2 for語句 17 4.3 range()函數(shù) 18 4.4 break和continue語句,以及循環(huán)中的else子句 19 4.5 pass 語句 20 4.6 定義函數(shù) 20 4.7 函數(shù)定義的更多形式 22 4.8 小插曲:編碼風(fēng)格 29 ['1 課前甜點 3', '2 使用Python解釋器 5', '2.1 調(diào)用解釋器 5', '2.1.1 傳入?yún)?shù) 6', '2.1.2 交互模式 6', '2.2 解釋器的運行環(huán)境 6', '2.2.1 源文件的字符編碼 6', '3 Python的非正式介紹 9', '3.1 Python作為計算器使用 9', '3.1.1 數(shù)字 9', '3.1.2 字符串 11', '3.1.3 列表 14', '3.2 走向編程的第一步 15', '4 其他流程控制工具 17', '4.1 if語句 17', '4.2 for語句 17', '4.3 range()函數(shù) 18', '4.4 break和continue語句,以及循環(huán)中的else子句 19', '4.5 pass 語句 20', '4.6 定義函數(shù) 20', '4.7 函數(shù)定義的更多形式 22', '4.8 小插曲:編碼風(fēng)格 29'] f.closed= True 進程已結(jié)束,退出代碼0
4、從txt中讀取目錄與頁碼并寫入PDF的書簽
# 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) # 標題的總個數(shù)
txt = []
for line in lines:
line = line.strip() # 去掉末尾的'\n'
pline = line.split(' ') # 根據(jù)line中' '進行分割
level = line.count('.') # 有n個'.'就是n+1級標題
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)運行結(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) # 標題的總個數(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中' '進行分割
level = line.count('.') # 有n個'.'就是n+1級標題
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)運行結(jié)果:

6、dir中沒有頁碼的情況
# 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) # 標題的總個數(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中' '進行分割
level = line.count('.') # 有n個'.'就是n+1級標題
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)運行結(jié)果

以上就是使用Python給PDF添加目錄書簽的實現(xiàn)方法的詳細內(nèi)容,更多關(guān)于Python給PDF添加目錄書簽的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python中用Descriptor實現(xiàn)類級屬性(Property)詳解
這篇文章主要介紹了Python中用Descriptor實現(xiàn)類級屬性(Property)詳解,本文先是講解了decorator是什么,然后給出了通過Descriptor來做一個類級的Property實例,需要的朋友可以參考下2014-09-09
Python數(shù)據(jù)結(jié)構(gòu)列表
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)列表,本文重點內(nèi)容主要是對列表數(shù)據(jù)結(jié)構(gòu)的使用,在Python中,序列是一組按順序排列的值。Python?有?3?種內(nèi)置的序列類型:字符串、?元組和列表,下面一起進入文章了解更詳細內(nèi)容吧,需要的小伙伴可以參考一下</P><P>2021-12-12
如何解決requests,已經(jīng)安裝卻無法import問題
這篇文章主要介紹了如何解決requests,已經(jīng)安裝卻無法import問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06

