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

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

python 复制代码
# -*- 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!')
相关推荐
心中有国也有家3 小时前
GE图引擎深度解析——CANN的计算图优化与执行引擎
人工智能·pytorch·python·学习·numpy
卷毛的技术笔记4 小时前
告别硬编码!Spring AI Alibaba 实现 AI Agent 智能工具调用(Tool Calling)
java·人工智能·后端·python·spring·ai编程
编程大师哥4 小时前
匿名函数 lambda + 高阶函数
java·python·算法
vb2008114 小时前
FastAPI APIRouter
开发语言·python
adrninistrat0r5 小时前
Java调用链MCP分析工具
java·python·ai编程
杨充5 小时前
1.3 浮点型数据设计灵魂
开发语言·python·算法
meilindehuzi_a6 小时前
深入浅出数据结构:Python 字典(Dict)与集合(Set)的哈希表底层全链路追踪
数据结构·python·散列表
Lucas凉皮6 小时前
20243408 2025-2026-2 《Python程序设计》综合实践报告
python·实验报告
键盘上的猫头鹰6 小时前
【MySQL 教程(八)】索引、事务、用户管理、导入导出与分页查询
数据库·python·mysql
薛定谔的猫-菜鸟程序员7 小时前
2小时智能体开发一个智能体?我用CodeArts Agent 和 AtomCode 开发了一个适老化智能体。
人工智能·python·agent