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 分钟前
Java程序员学Python学习笔记一:学习python的动机与思考
java·后端·python
电院工程师23 分钟前
SM3算法Python实现(无第三方库)
开发语言·python·算法·安全·密码学
CodeDevMaster1 小时前
在Jupyter Notebook中使用Conda虚拟环境
python·jupyter
冷月半明1 小时前
告别手动拖动!Python+dddocr自动化破解多缺口滑块
python
Kusunoki_D1 小时前
Python 实现 Web 静态服务器(HTTP 协议)
服务器·前端·python
站大爷IP2 小时前
当Python遇上多线程:ThreadPoolExecutor的实用指南
python
站大爷IP2 小时前
Python文件操作的“保险箱”:with语句深度实战指南
python
探模之翼2 小时前
高效管理Python环境:Miniforge、pyenv和Poetry深度对比与应用
python
Tony11542 小时前
win11系统部署tomcat10教程
windows·tomcat
橘子夏与单车少年k2 小时前
疏锦行Python打卡 DAY 27 函数专题2:装饰器
开发语言·python