博客
关于我
使用Python自由切分pdf文件提取任意页面
阅读量:279 次
发布时间:2019-03-01

本文共 764 字,大约阅读时间需要 2 分钟。

推荐教材:《Python程序设计基础与应用》(ISBN:9787111606178),董付国,机械工业出版社。

问题描述:给定一个PDF文件,对其进行任意切分,提取其中任意页面,保存为新的PDF文件。

准备工作:安装扩展库PyPDF2,参考命令pip install PyPDF2。

代码示例:

import PyPDF2def extract_pages(pdf_path):    # 读取PDF文件    pdf = PyPDF2.PdfReader(pdf_path)    # 提取每一页    pages = []    for page in pdf.pages:        pages.append(page)    return pages# 示例使用if __name__ == "__main__":    import sys    input_path = sys.argv[1]    pages = extract_pages(input_path)    # 保存为新PDF文件    output_path = "extracted_pages.pdf"    with open(output_path, 'wb') as output:        for page in pages:            output.write(page.get_data())    print(f"提取后的PDF文件已保存为:{output_path}")

配套资源:教师可联系董付国老师获取教学大纲、课件、源码、电子教案、考试系统等配套教学资源。

温馨提示:在公众号后台发送消息"大事记"、"教材"、"历史文章"、"会议"、"培训"、"微课"、"课件"、"小屋刷题"可获取更多资源和信息。

转载地址:http://payx.baihongyu.com/

你可能感兴趣的文章
PID控制器数字化
查看>>
Qwen-VL项目使用指南
查看>>
PIESDKDoNet二次开发配置注意事项
查看>>
PIGS POJ 1149 网络流
查看>>
PIL Image对图像进行点乘,加上常数(等像素操作)
查看>>
PIL Image转Pytorch Tensor
查看>>
PIL&QOOT;IOERROR:带有大图像的图像文件被截断(&Q)
查看>>
PIL.Image、cv2的img、bytes相互转换
查看>>
PIL.Image进行图像融合显示(Image.blend)
查看>>
pilicat-dfs 霹雳猫-分布式文件系统
查看>>
Pillow lacks the JPEG 2000 plugin
查看>>
SpringBoot之ElasticsearchRestTemplate常用示例
查看>>
ping 全网段CMD命令
查看>>
ping 命令的七种用法,看完瞬间成大神
查看>>
Pinia入门(快速上手)
查看>>
Pinia:$patch的使用场景
查看>>
Pinia:$subscribe()的使用场景
查看>>
Pinpoint对Kubernetes关键业务模块进行全链路监控
查看>>
Pinterest 大规模缓存集群的架构剖析
查看>>
pintos project (2) Project 1 Thread -Mission 1 Code
查看>>