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

你可能感兴趣的文章
nodejs libararies
查看>>
nodejs-mime类型
查看>>
nodejs中Express 路由统一设置缓存的小技巧
查看>>
Node入门之创建第一个HelloNode
查看>>
NOIp2005 过河
查看>>
NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
查看>>
npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
查看>>
npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
查看>>
npm前端包管理工具简介---npm工作笔记001
查看>>
npm和yarn的使用对比
查看>>
npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
查看>>
npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
查看>>
NPOI利用多任务模式分批写入多个Excel
查看>>
NR,NF,FNR
查看>>
nrf开发笔记一开发软件
查看>>
NSDateFormatter的替代方法
查看>>
nsis 安装脚本示例(转)
查看>>
NSOperation基本操作
查看>>
NSSet集合 无序的 不能重复的
查看>>
NT AUTHORITY\NETWORK SERVICE 权限问题
查看>>