当前位置: 首页 > news >正文

python读取PDF文件中的指定页码的范围并存储到指定的文件名

读取PDF文件中的指定页码的范围并存储到指定的文件名

# -*- coding: utf-8 -*-
"""
Created on Mon Nov 27 21:36:12 2023

@author: cnliu
pip install pypdf2  #安装pypdf2  --3.o版
"""
from PyPDF2 import PdfWriter, PdfReader
import os
 
#pathToPDF = input('something like /home/pedro/Latin/ ... ')
pathToPDF = "d:/书/"
path2Extracts = 'd:/'
# get the names of the files available to extract from
files = os.listdir(pathToPDF)
# show the files in a loop so you can choose 1
# I haven't done that here
# choose a PDF from a list of PDFs from  as bookname
#bookTitle = bookname.replace('.pdf', '')
bookname = "AHaSuanFa.pdf"
# read the pdf
pdf = PdfReader(pathToPDF + bookname)
#pages = pdf.getNumPages() (deprecated)
pages = len(pdf.pages)
print('This pdf has ' + str(pages) + ' pages')
print('What pages do you want to get?')
startnum = input('what is the starting page number?  ')
print('If your last page is page 76, enter 76 for the end number')
endnum = input('what is the last page number?  ')
start = int(startnum) - 1
end = int(endnum)
# only need to open pdfWriter 1 time
pdf_writer = PdfWriter()
for page in range(start, end):
        pdf_writer.add_page(pdf.pages[page])
         
print('Enter the savename for this pdf, like CE3U8')
savename = input('Enter the name to save this pdf under, like CE3U8 No need to add .pdf ... ')
output_filename = savename + '.pdf'
 
with open(path2Extracts + output_filename, 'wb') as out:
        pdf_writer.write(out)
print(f'Created: {output_filename} and saved in', path2Extracts)
print('All done!')
http://www.dtcms.com/a/4801.html

相关文章:

  • Java中的Integer.bitCount浅析
  • 初识JVM(简单易懂),解开JVM神秘的面纱
  • C语言面试之旅:掌握基础,探索深度(面试实战之c语言关键词中篇)
  • 04_Flutter自定义Slider滑块
  • filebeat(远程收集日志工具)
  • 大模型训练为什么用A100不用4090
  • Anaconda离线下载torch与安装包
  • QT配合CSS隐藏按钮
  • 建筑红模板尺寸规格
  • Python web自动化测试 —— 文件上传
  • 18、Android 组件化
  • 搭个网页应用,让ChatGPT帮我写SQL
  • 【传送点】上千漏洞复现复现集合 exp poc 持续更新
  • 微信小程序记住密码,让登录解放双手
  • Redis key的类型以及命令
  • 你要的fiddler快捷键全部在这里了,学最全的快捷键,做最快的IT程序员
  • mysql中数据是如何被用B+树查询到的
  • 高性能无锁队列 moodycamel::ConcurrentQueue
  • Elasticsearch底层原理分析——新建、索引文档
  • Docker
  • C#中的迭代器和分部类
  • 2分钟快速实现非逻辑卷磁盘扩容
  • 异步组件与函数式组件
  • matlab提取特征(医学图像)
  • [架构之路-248]:目标系统 - 设计方法 - 软件工程 - 需求工程- 需求开发:如何用图形表达需求,结构化需求分析与面向对象需求分析的比较与融合
  • Flask Web开发实验一:第一个Flask项目与Flask的工作方式
  • Android 打包aar包含第三方aar 解决方案
  • 最新发布 Spring Boot 3.2.0 新特性和改进
  • 防爆智能安全帽、防爆手持终端,防爆智能矿灯守护安全,在煤矿安全生产远程可视化监管中的应用
  • 蓝桥杯day01——负二进制数相加