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

你可能感兴趣的文章
notepad++最详情汇总
查看>>
notepad++正则表达式替换字符串详解
查看>>
notepad如何自动对齐_notepad++怎么自动排版
查看>>
Notes on Paul Irish's "Things I learned from the jQuery source" casts
查看>>
Notification 使用详解(很全
查看>>
NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
查看>>
NotImplementedError: Could not run torchvision::nms
查看>>
nova基于ubs机制扩展scheduler-filter
查看>>
Now trying to drop the old temporary tablespace, the session hangs.
查看>>
nowcoder—Beauty of Trees
查看>>
np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
查看>>
np.power的使用
查看>>
NPM 2FA双重认证的设置方法
查看>>
npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
查看>>
npm build报错Cannot find module ‘webpack‘解决方法
查看>>
npm ERR! ERESOLVE could not resolve报错
查看>>
npm ERR! fatal: unable to connect to github.com:
查看>>
npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
查看>>
npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
查看>>
npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
查看>>