最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Python給PDF添加目錄書簽的實現(xiàn)方法

 更新時間:2023年10月06日 09:55:28   作者:飛由于度  
有時下載到掃描版的 PDF 是不帶書簽?zāi)夸浀?這樣閱讀起來很不方便,下面通過 python 實現(xiàn)一個半自動化添加書簽?zāi)夸浀哪_本,文中通過代碼介紹的非常詳細,具有一定的參考價值,需要的朋友可以參考下

0、庫的選擇——pypdf

原因:Python Version Support

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中多線程及程序鎖淺析

    Python中多線程及程序鎖淺析

    這篇文章主要介紹了Python中多線程及程序鎖淺析,本文用一個實例講解Python的多線程和程序鎖,需要的朋友可以參考下
    2015-01-01
  • Django入門優(yōu)缺點及環(huán)境搭建流程

    Django入門優(yōu)缺點及環(huán)境搭建流程

    這篇文章主要為大家介紹了Django入門優(yōu)缺點及環(huán)境搭建流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-11-11
  • Python中用Descriptor實現(xiàn)類級屬性(Property)詳解

    Python中用Descriptor實現(xiàn)類級屬性(Property)詳解

    這篇文章主要介紹了Python中用Descriptor實現(xiàn)類級屬性(Property)詳解,本文先是講解了decorator是什么,然后給出了通過Descriptor來做一個類級的Property實例,需要的朋友可以參考下
    2014-09-09
  • 支持python的分布式計算框架Ray詳解

    支持python的分布式計算框架Ray詳解

    Ray是一種分布式執(zhí)行框架,便于大規(guī)模應(yīng)用程序和利用先進的機器學(xué)習(xí)庫,今天給大家分享支持python的分布式計算框架Ray詳解,感興趣的朋友一起看看吧
    2021-07-07
  • Python?NumPy隨機抽模塊介紹及方法

    Python?NumPy隨機抽模塊介紹及方法

    這篇文章主要介紹了Python?NumPy隨機抽模塊介紹及方法,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下
    2022-09-09
  • Python實現(xiàn)vlog生成器的示例代碼

    Python實現(xiàn)vlog生成器的示例代碼

    vlog,全稱為Video?blog,意為影音博客,也有翻譯為微錄。本文將嘗試用Python基于Moviepy從一個文本文件中自動生成一個視頻格式的vlog,感興趣的可以了解一下
    2023-01-01
  • python如何繪制路段時變車速熱力圖

    python如何繪制路段時變車速熱力圖

    本文通過熱力圖形式展示了24小時內(nèi)某個路段的車速變化和特定時刻某條路徑的車速情況,數(shù)據(jù)是通過Numpy隨機生成的,用以模擬真實的車速情況,文章還展示了如何利用pandas和seaborn庫中的pivot_table()和heatmap()函數(shù)生成熱力圖
    2024-09-09
  • Python數(shù)據(jù)結(jié)構(gòu)列表

    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
  • python寫入Excel表格的方法詳解

    python寫入Excel表格的方法詳解

    這篇文章主要為大家詳細介紹了python寫入Excel表格的方法,使用jupyter?notebook,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • 如何解決requests,已經(jīng)安裝卻無法import問題

    如何解決requests,已經(jīng)安裝卻無法import問題

    這篇文章主要介紹了如何解決requests,已經(jīng)安裝卻無法import問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06

最新評論

视频| 河南省| 栾城县| 惠安县| 罗山县| 太谷县| 弥勒县| 铜梁县| 富蕴县| 武鸣县| 登封市| 鹰潭市| 昌平区| 定安县| 沿河| 大港区| 岳阳市| 临澧县| 织金县| 府谷县| 双辽市| 乐平市| 建平县| 千阳县| 德江县| 金沙县| 永泰县| 望江县| 同江市| 博野县| 德江县| 包头市| 台山市| 四子王旗| 北辰区| 灵山县| 三都| 平原县| 东安县| 万安县| 祥云县|