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

你可能感兴趣的文章
npm和yarn的使用对比
查看>>
npm如何清空缓存并重新打包?
查看>>
npm学习(十一)之package-lock.json
查看>>
npm安装 出现 npm ERR! code ETIMEDOUT npm ERR! syscall connect npm ERR! errno ETIMEDOUT npm ERR! 解决方法
查看>>
npm安装crypto-js 如何安装crypto-js, python爬虫安装加解密插件 找不到模块crypto-js python报错解决丢失crypto-js模块
查看>>
npm安装教程
查看>>
npm报错Cannot find module ‘webpack‘ Require stack
查看>>
npm报错Failed at the node-sass@4.14.1 postinstall script
查看>>
npm报错fatal: Could not read from remote repository
查看>>
npm报错File to import not found or unreadable: @/assets/styles/global.scss.
查看>>
npm报错TypeError: this.getOptions is not a function
查看>>
npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
查看>>
npm淘宝镜像过期npm ERR! request to https://registry.npm.taobao.org/vuex failed, reason: certificate has ex
查看>>
npm版本过高问题
查看>>
npm的“--force“和“--legacy-peer-deps“参数
查看>>
npm的安装和更新---npm工作笔记002
查看>>
npm的常用操作---npm工作笔记003
查看>>
npm的常用配置项---npm工作笔记004
查看>>
npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
查看>>
npm编译报错You may need an additional loader to handle the result of these loaders
查看>>