背景:由于某些缘故,一个大的pdf需要拆分成页数少的pdf,或者pdf需要去掉指定页,那么就有必要对pdf进行重新编辑,这里需要用到一个库,直接进行操作即可。
当使用Python时,可以使用PyMuPDF库来拆分PDF文件。以下是一个示例代码,
import fitz # PyMuPDF
def split_pdf(input_file, output_folder):
pdf_document = fitz.open(input_file)
for page_number in range(pdf_document.page_count):
page = pdf_document[page_number]
output_file = f"{output_folder}/page_{page_number + 1}.pdf"
page.write(output_file)
pdf_document.close()
input_file = "input.pdf"
output_folder = "output"
split_pdf(input_file, output_folder)
翻译成英文如下:
use the PyPDF2 library to split a PDF file in Python. Here's an example code that splits a PDF file into individual pages:
In this code, the `split_pdf` function takes the