使用Python給PDF添加目錄書簽的實(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、添加書簽——方法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)需要添加書簽的PDF writer = pypdf.PdfWriter() # 創(chuàng)建一個(gè)PdfWriter類 writer.append(input1) # 將PDF讀入writer中,然后進(jìn)行書簽的編輯 writer.add_outline_item(title='10', page_number=10, parent=None) # 添加第一個(gè)書簽 writer.add_outline_item(title='11', page_number=11, parent=None) # 添加第二個(gè)書簽 # Write to an output PDF document output = open('01_' + wk_in_file_name, "wb") # 如果wk_out_file_name不存在,則創(chuàng)建一個(gè) writer.write(output) # 將添加書簽后的PDF保存 # Close File Descriptors writer.close() output.close() print('pypdf.__version__=', pypdf.__version__) print('sys.version=', sys.version) pass
運(yùn)行結(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) # 添加第一個(gè)書簽 writer.add_outline_item(title='10_1', page_number=11, parent=parent_bookmark_0) # 添加第一個(gè)書簽的子書簽 parent_bookmark_1 = writer.add_outline_item(title='11', page_number=20, parent=None) # 添加第二個(gè)書簽 writer.add_outline_item(title='11_1', page_number=21, parent=parent_bookmark_1) # 添加第二個(gè)書簽的子書簽 # 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è)碼并寫入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) # 標(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í)現(xiàn)方法的詳細(xì)內(nèi)容,更多關(guān)于Python給PDF添加目錄書簽的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python OpenCV基于霍夫圈變換算法檢測(cè)圖像中的圓形
這篇文章主要介紹了通過(guò)霍夫圈變換算法檢測(cè)圖像中的圓形,文中用到的函數(shù)為cv2.HoughCircles(),該函數(shù)可以很好地檢測(cè)圓心。感興趣的小伙伴可以了解一下2021-12-12Selenium獲取登錄Cookies并添加Cookies自動(dòng)登錄的方法
這篇文章主要介紹了Selenium獲取登錄Cookies并添加Cookies自動(dòng)登錄的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12networkx庫(kù)繪制帶權(quán)圖給無(wú)權(quán)圖加權(quán)重輸出
這篇文章主要為大家介紹了Python?networkx庫(kù)繪制帶權(quán)圖給無(wú)權(quán)圖加權(quán)重并輸出權(quán)重的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Python中強(qiáng)大的命令行庫(kù)click入門教程
click是Python的一個(gè)命令行工具,極其好用。不信?一試便知。下面這篇文章主要給大家介紹了Python中強(qiáng)大的命令行庫(kù)click,需要的朋友可以參考學(xué)習(xí),下面來(lái)一起看看吧。2016-12-12PyCharm運(yùn)行python測(cè)試,報(bào)錯(cuò)“沒(méi)有發(fā)現(xiàn)測(cè)試”/“空套件”的解決
這篇文章主要介紹了PyCharm運(yùn)行python測(cè)試,報(bào)錯(cuò)“沒(méi)有發(fā)現(xiàn)測(cè)試”/“空套件”的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01Python 字符串操作實(shí)現(xiàn)代碼(截取/替換/查找/分割)
這篇文章主要介紹了Python 字符串截取/替換/查找/分割等實(shí)現(xiàn)方法,需要的朋友可以參考下2013-06-06基于Python實(shí)現(xiàn)帕累托圖的示例詳解
帕累托圖是一種特殊的直方圖, 在項(xiàng)目管理知識(shí)體系中屬于質(zhì)量管理的工具。本文為大家整理了Python實(shí)現(xiàn)帕累托圖的方法,需要的可以參考一下2023-03-03基于python實(shí)現(xiàn)藍(lán)牙通信代碼實(shí)例
這篇文章主要介紹了基于python實(shí)現(xiàn)藍(lán)牙通信代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11基于python實(shí)現(xiàn)復(fù)制文件并重命名
這篇文章主要介紹了基于python實(shí)現(xiàn)復(fù)制文件并重命名,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09