发现有时候pdf需要切分,以便满足各种大小和上传要求,因此用python写了一个,方便将pdf按页码均等切分。
import PyPDF2
import os
def split_pdf(input_pdf_path, num_splits):
# Create a PDF reader object
pdf_reader = PyPDF2.PdfReader(open(input_pdf_path, 'rb'))
total_pages = len(pdf_reader.pages)
# Calculate the number of pages per split
pages_per_split = total_pages // num_splits
# Get the directory and base name of the input PDF
base_dir = os.path.dirname(input_pdf_path)
base_name = os.path.splitext(os.path.basename(input_pdf_path))[0]
for i in range(num_splits):
pdf_writer = PyPDF2.PdfWriter()
start_page = i * pages_per_split
end_page = start_page + pages_per_split
if i == num_splits - 1: # Make sure to include remaining pages in the last split
end_page = total_pages
for page in range(start_page, end_page):
pdf_writer.add_page(pdf_reader.pages[page])
output_pdf_path = os.path.join(base_dir, f"{base_name}_part_{i + 1}.pdf")
with open(output_pdf_path, 'wb') as output_pdf:
pdf_writer.write(output_pdf)
print(f"PDF split into {num_splits} parts successfully.")
# Example usage
input_pdf_path = 'C:/XXX/XXX.pdf' # 文件路径
num_splits = 4 # 切分数量
split_pdf(input_pdf_path, num_splits)