测试了一下 这个可以[Python] 纯文本查看 复制代码 import PyPDF2
import os
def remove_header_footer(input_pdf_path, output_pdf_path):
with open(input_pdf_path, 'rb') as file:
reader = PyPDF2.PdfReader(file)
writer = PyPDF2.PdfWriter()
for page in reader.pages:
# 仔细调整裁剪框的参数,以去除页眉和页脚
original_mediabox = page.mediabox
# 这里将页眉和页脚分别设置为50单位,可以根据需要调整
page.cropbox.lower_left = (original_mediabox.lower_left[0], original_mediabox.lower_left[1] + 50) # 设置下边界
page.cropbox.upper_right = (original_mediabox.upper_right[0], original_mediabox.upper_right[1] - 50) # 设置上边界
writer.add_page(page) # 将处理后的页面添加到新的PDF中
with open(output_pdf_path, 'wb') as output_file:
writer.write(output_file)
# 批量处理指定目录下的PDF文件
def batch_process_pdfs(input_directory, output_directory):
if not os.path.exists(output_directory):
os.makedirs(output_directory)
for filename in os.listdir(input_directory):
if filename.endswith('.pdf'):
input_path = os.path.join(input_directory, filename)
output_path = os.path.join(output_directory, filename)
remove_header_footer(input_path, output_path)
print(f"Processed {filename}")
# 输入输出目录
input_directory = 'D:/222/input/directory' # 替换为您的输入目录
output_directory = 'D:/222/output/directory' # 替换为您的输出目录
# 执行批量处理
batch_process_pdfs(input_directory, output_directory) |