真是操了,某办公软件连pdf的指定页数删除都要VIP。
import os
from PyPDF2 import PdfReader, PdfWriter
def process_pdf(input_path, start_page_to_remove=None, end_page_to_remove=None, output_folder=None):
try:
# 验证输入文件
if not os.path.isfile(input_path):
raise FileNotFoundError("输入文件不存在")
if not input_path.lower().endswith('.pdf'):
raise ValueError("输入文件必须是PDF格式")
# 读取PDF文件
with open(input_path, 'rb') as input_file:
reader = PdfReader(input_file)
total_pages = len(reader.pages)
# 验证页面范围
if start_page_to_remove is not None:
if start_page_to_remove < 0 or start_page_to_remove >= total_pages:
raise ValueError(f"起始页必须在0-{total_pages - 1}范围内")
if end_page_to_remove is not None:
if end_page_to_remove < 0 or end_page_to_remove >= total_pages:
raise ValueError(f"结束页必须在0-{total_pages - 1}范围内")
if start_page_to_remove is not None and end_page_to_remove is not None:
if start_page_to_remove > end_page_to_remove:
raise ValueError("起始页不能大于结束页")
# 创建PDF写入器
writer = PdfWriter()
# 复制保留的页面
pages_to_remove = set()
if start_page_to_remove is not None and end_page_to_remove is not None:
pages_to_remove = set(range(start_page_to_remove, end_page_to_remove + 1))
for page_num in range(total_pages):
if page_num not in pages_to_remove:
writer.add_page(reader.pages[page_num])
# 准备输出路径
if output_folder is None:
output_folder = os.path.dirname(input_path)
elif not os.path.exists(output_folder):
os.makedirs(output_folder)
base_name = os.path.splitext(os.path.basename(input_path))[0]
output_path = os.path.join(output_folder, f"{base_name}_processed.pdf")
# 写入输出文件
with open(output_path, 'wb') as output_file:
writer.write(output_file)
print(f"处理成功! 保存到: {output_path}")
print(f"原始页数: {total_pages}, 处理后页数: {total_pages - len(pages_to_remove)}")
return output_path
except Exception as e:
print(f"处理失败: {str(e)}")
return None
if __name__ == "__main__":
print("PDF页面删除工具")
print("=" * 40)
# 获取用户输入
input_path = input("输入PDF文件路径: ").strip()
start_page = input("删除起始页(从0开始, 留空则不删除): ").strip()
start_page = int(start_page) if start_page else None
end_page = input("删除结束页(包含, 留空则不删除): ").strip()
end_page = int(end_page) if end_page else None
output_folder = input("保存路径(留空则保存到原目录): ").strip()
output_folder = output_folder if output_folder else None
# 处理文件
process_pdf(
input_path=input_path,
start_page_to_remove=start_page,
end_page_to_remove=end_page,
output_folder=output_folder
)
1029

被折叠的 条评论
为什么被折叠?



