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的使用
python
# 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() # 创建一个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不存在,则创建一个
writer.write(output) # 将添加书签后的PDF保存
# Close File Descriptors
writer.close()
output.close()
print('pypdf.__version__=', pypdf.__version__)
print('sys.version=', sys.version)
pass
运行结果
2、添加子书签------参数parent的使用
python
# 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
运行结果
3、读取txt文件
python
# 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) # 标题的总个数
txt = []
for line in lines:
txt.append(line.strip())
print(line.strip())
line.strip() # 去掉末尾的'\n'
line.split(' ') # 根据line中' '进行分割
line.count('.') # 有n个'.'就是n+1级标题
print(txt)
f.close() # 关闭文件
print('f.closed=', f.closed)
运行结果
python
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 调用解释器 5
2.1.1 传入参数 6
2.1.2 交互模式 6
2.2 解释器的运行环境 6
2.2.1 源文件的字符编码 6
3 Python的非正式介绍 9
3.1 Python作为计算器使用 9
3.1.1 数字 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()函数 18
4.4 break和continue语句,以及循环中的else子句 19
4.5 pass 语句 20
4.6 定义函数 20
4.7 函数定义的更多形式 22
4.8 小插曲:编码风格 29
['1 课前甜点 3', '2 使用Python解释器 5', '2.1 调用解释器 5', '2.1.1 传入参数 6', '2.1.2 交互模式 6', '2.2 解释器的运行环境 6', '2.2.1 源文件的字符编码 6', '3 Python的非正式介绍 9', '3.1 Python作为计算器使用 9', '3.1.1 数字 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()函数 18', '4.4 break和continue语句,以及循环中的else子句 19', '4.5 pass 语句 20', '4.6 定义函数 20', '4.7 函数定义的更多形式 22', '4.8 小插曲:编码风格 29']
f.closed= True
进程已结束,退出代码0
4、从txt中读取目录与页码并写入PDF的书签
python
# 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) # 标题的总个数
txt = []
for line in lines:
line = line.strip() # 去掉末尾的'\n'
pline = line.split(' ') # 根据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() # 关闭文件
print('f.closed=', f.closed)
运行结果
5、添加偏置
python
# 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) # 标题的总个数
offset = 5 # 添加偏置
txt = []
bookmark_parent_0 = None
bookmark_parent_1 = None
for line in lines:
line = line.strip() # 去掉末尾的'\n'
pline = line.split(' ') # 根据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() # 关闭文件
print('f.closed=', f.closed)
运行结果:
6、dir中没有页码的情况
python
# 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) # 标题的总个数
offset = 5 # 添加偏置
txt = []
bookmark_parent_0 = None
bookmark_parent_1 = None
for line in lines:
line = line.strip() # 去掉末尾的'\n'
pline = line.split(' ') # 根据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() # 关闭文件
print('f.closed=', f.closed)
运行结果
参考
使用 python 给 PDF 添加目录书签_pypdf2.errors.deprecationerror: pdffilereader is d_Wreng我是002的博客-CSDN博客