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!')
相关推荐
醒了就刷牙1 分钟前
Hugging_Face实战
python
Volunteer Technology2 分钟前
LangGraph的Agent的上下文
人工智能·后端·python·langchain
luoluoal8 分钟前
基于python的医疗知识图谱问答系统(源码+文档)
python·mysql·django·毕业设计·源码
小比特_蓝光11 分钟前
STL小知识点——C++
java·开发语言·c++·python
I'm Jie15 分钟前
【已解决】SqlAlchemy 插入 MySQL JSON 字段时 None 变为 ‘null‘ 字符串,WHERE IS NULL 失效
数据库·python·mysql·json·fastapi·sqlalchemy
郝学胜-神的一滴31 分钟前
Python中的Dict子类:优雅扩展字典的无限可能
开发语言·python
二十雨辰36 分钟前
[python]-面向对象高级
python
dust_and_stars39 分钟前
Windows 11 RDP Wrapper 配置教程与常见问题解决方法
运维·windows
向量引擎小橙1 小时前
从“对话助手”到“数字架构师”:Claude 4.6 Opus 如何凭一己之力,终结全球程序员的“CRUD 焦虑”?
人工智能·python·gpt·深度学习
小鸡吃米…1 小时前
TensorFlow - 单层感知机
人工智能·python·tensorflow