[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:
-
Install PyMuPDF:
pip install PyMuPDF -
Import the necessary library:
import fitz # PyMuPDF -
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:
-
Install PyMuPDF:
pip install PyMuPDF This command installs the
PyMuPDF
library. -
Import the necessary library:
import fitz # PyMuPDF This import brings in the
fitz
module from thePyMuPDF
library. -
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! 😊
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球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】论文查重修改技巧大全