Typora導出的PDF目錄標題自動加編號
在Typora主題文件夾增加如下文件后,標題便自動加上了編號:
https:///as604049322/blog_data/-/blob/master/base.user.css
例如:
但是導出的PDF中,目錄卻沒有編號:
這是我使用Python處理該文件,使其具有編號,完整代碼如下:
# 博客地址:https://blog.csdn.net/as604049322
__author__ = '小小明-代碼實體'
__date__ = '2023/8/31'
from PyPDF2 import PdfReader, PdfWriter
def get_pdf_Bookmark(filename):
"作者CSDN:https://blog.csdn.net/as604049322"
if isinstance(filename, str):
pdf_reader = PdfReader(filename)
else:
pdf_reader = filename
pagecount = len(pdf_reader.pages)
# 用保存每個標題id所對應的頁碼
idnum2pagenum = {}
for i in range(pagecount):
page = pdf_reader.pages[i]
idnum2pagenum[page.indirect_ref.idnum] = i
# 保存每個標題對應的標簽數據,包括層級,標題和頁碼索引(頁碼-1)
bookmark = []
def get_pdf_Bookmark_inter(outlines, tab=0):
for outline in outlines:
if isinstance(outline, list):
get_pdf_Bookmark_inter(outline, tab + 1)
else:
bookmark.append(
(tab, outline['/Title'], idnum2pagenum[outline.page.idnum]))
get_pdf_Bookmark_inter(pdf_reader.outline)
return bookmark
def pdf_write_bookmark(bookmark, pdf_file, compress=True):
pdf_reader = PdfReader(pdf_file)
num_pages = len(pdf_reader.pages)
pdf_writer = PdfWriter()
for page in pdf_reader.pages:
if compress:
page.compress_content_streams()
pdf_writer.add_page(page)
# pdf_reader.
last_cache = [None] * (max(bookmark, key=lambda x: x[0])[0] + 1)
for tab, title, pagenum in bookmark:
if pagenum >= num_pages:
continue
parent = last_cache[tab - 1] if tab > 0 else None
indirect_id = pdf_writer.add_outline_item(title, pagenum, parent=parent)
last_cache[tab] = indirect_id
pdf_writer.page_mode = "/UseOutlines"
with open(pdf_file, "wb") as out:
pdf_writer.write(out)
print("已成功將書簽寫入到", pdf_file)
if __name__ == '__main__':
file = r"C:\Users\sj\Desktop\集團管理層培訓.pdf"
bookmark = get_pdf_Bookmark(file)
num_cache = [0] * 7
last_tab = 0
new_bookmark = []
for tab, title, pagenum in bookmark:
if tab > last_tab:
num_cache[tab] = 1
else:
num_cache[tab] += 1
new_title = title
if not title[0].isdigit():
new_title = ".".join(map(str, num_cache[:tab + 1])) + " " + title
# print(tab, new_title, pagenum)
new_bookmark.append((tab, new_title, pagenum))
last_tab = tab
pdf_write_bookmark(new_bookmark, file)
處理后的PDF目錄就有編號了: