alex_bn_lee

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

[1117] Remove some pages from a PDF while preserving the bookmarks

To remove the last page from a PDF while preserving the bookmarks, you can use the PyMuPDF library (fitz package) instead of PyPDF2, as PyMuPDF provides better support for handling bookmarks. Here’s how you can do it:

  1. Install PyMuPDF:

    pip install PyMuPDF
  2. Import the necessary library:

    import fitz # PyMuPDF
  3. Remove the last page while keeping the bookmarks:

    def remove_last_page_keep_bookmarks(input_pdf_path, output_pdf_path):
    # Open the original PDF
    pdf_document = fitz.open(input_pdf_path)
    # Remove the last page
    pdf_document.delete_page(pdf_document.page_count - 1)
    # Save the modified PDF to a new file
    pdf_document.save(output_pdf_path)
    # Example usage
    input_pdf_path = 'path/to/your/input.pdf'
    output_pdf_path = 'path/to/your/output.pdf'
    remove_last_page_keep_bookmarks(input_pdf_path, output_pdf_path)

Explanation:

  1. Install PyMuPDF:

    pip install PyMuPDF

    This command installs the PyMuPDF library.

  2. Import the necessary library:

    import fitz # PyMuPDF

    This import brings in the fitz module from the PyMuPDF library.

  3. Remove the last page while keeping the bookmarks:

    def remove_last_page_keep_bookmarks(input_pdf_path, output_pdf_path):
    pdf_document = fitz.open(input_pdf_path)
    pdf_document.delete_page(pdf_document.page_count - 1)
    pdf_document.save(output_pdf_path)
    • fitz.open(): Opens the original PDF file.
    • delete_page(): Deletes the last page from the PDF (the index is page_count - 1 because the pages are 0-indexed).
    • save(): Saves the modified PDF to a new file, preserving the existing bookmarks.

Example: Remove the last page

import fitz # PyMuPDF
def remove_last_page_keep_bookmarks(input_pdf_path, output_pdf_path):
# Open the original PDF
pdf_document = fitz.open(input_pdf_path)
# Remove the last page
pdf_document.delete_page(pdf_document.page_count - 1)
# Save the modified PDF to a new file
pdf_document.save(output_pdf_path)
# Example usage
input_pdf_path = 'path/to/your/input.pdf'
output_pdf_path = 'path/to/your/output.pdf'
remove_last_page_keep_bookmarks(input_pdf_path, output_pdf_path)

Example: Rmove the last four pages

import fitz # PyMuPDF
def remove_last_page_keep_bookmarks(input_pdf_path, output_pdf_path):
# Open the original PDF
pdf_document = fitz.open(input_pdf_path)
# Remove the last four pages
for _ in range(4):
pdf_document.delete_page(pdf_document.page_count - 1)
# Save the modified PDF to a new file
pdf_document.save(output_pdf_path)
# Example usage
input_pdf_path = 'path/to/your/input.pdf'
output_pdf_path = 'path/to/your/output.pdf'
remove_last_page_keep_bookmarks(input_pdf_path, output_pdf_path)

This code will remove the last page from the specified PDF file while preserving the existing bookmarks.

If you have any further questions or need additional assistance, feel free to ask! 😊

posted on   McDelfino  阅读(4)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
历史上的今天:
2024-03-05 [982] Print and save a JSON file with indentation in Python
2014-03-05 【139】论文查重修改技巧大全
点击右上角即可分享
微信分享提示