Typora导出的PDF目录标题自动加编号

Typora导出的PDF目录标题自动加编号

在Typora主题文件夹增加如下文件后,标题便自动加上了编号:

https://gitcode.net/as604049322/blog_data/-/blob/master/base.user.css

例如:

但是导出的PDF中,目录却没有编号:

这是我使用Python处理该文件,使其具有编号,完整代码如下:

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目录就有编号了:

相关推荐
小陳参上3 小时前
用Python创建一个Discord聊天机器人
jvm·数据库·python
minstbe5 小时前
IC设计私有化AI助手实战:基于Docker+OpenCode+Ollama的数字前端综合增强方案(进阶版)
人工智能·python·语言模型·llama
zyq99101_17 小时前
优化二分查找:前缀和降复杂度
数据结构·python·蓝桥杯
qyzm7 小时前
天梯赛练习(3月13日)
开发语言·数据结构·python·算法·贪心算法
Qt学视觉8 小时前
AI2-Paddle环境搭建
c++·人工智能·python·opencv·paddle
廋到被风吹走8 小时前
【LangChain4j】特点功能及使用场景
后端·python·flask
Eward-an8 小时前
LeetCode 239. 滑动窗口最大值(详细技术解析)
python·算法·leetcode
喵手9 小时前
Python爬虫实战:用代码守护地球,追踪WWF濒危物种保护动态!
爬虫·python·爬虫实战·濒危物种·零基础python爬虫教学·wwf·濒危物种保护动态追踪
梦想的旅途29 小时前
如何通过 QiWe API 实现企业微信主动发消息
开发语言·python
喵手9 小时前
Python爬虫实战:自动化抓取 Pinterest 热门趋势与创意!
爬虫·python·爬虫实战·pinterest·零基础python爬虫教学·采集pinterest热门趋势·热门趋势预测