博客
关于我
使用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/

你可能感兴趣的文章
Prometheus 云原生 - 监控 Linux、MySQL、Redis、RabbitMQ、Docker、SpringBoot 3.x
查看>>
Prometheus 介绍
查看>>
Prometheus 安全配置详解
查看>>
prometheus 安装node_exporter, node_exporter 安装最新版 普罗米修思安装监控服务器client
查看>>
Prometheus 安装部署实战
查看>>
prometheus 持久化存储方案
查看>>
Prometheus 监控系统企业级实战
查看>>
Prometheus 监控系统简介
查看>>
Prometheus 配置详解
查看>>
Prometheus 采集器使用详解
查看>>
Prometheus 黑盒监控实战
查看>>
prometheus+alertmanager+grafana监控部署教程
查看>>
Prometheus+Grafana构建智能化Kubernetes监控系统实战
查看>>
Prometheus+SpringBoot应用监控全过程详解
查看>>
Prometheus+SpringBoot应用监控全过程详解
查看>>
prometheus安装
查看>>
Prometheus实战教程:监控Kafka消息
查看>>
Prometheus实战教程:监控mysql数据库
查看>>
Prometheus实战教程:监控Nginx状态
查看>>
prometheus常用exporter下载地址大全
查看>>