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!')
相关推荐
青 春 记 忆6 小时前
零基础入门python30:Flask个人账本从空目录运行与阶段验收
python·flask·后端开发
l1258656 小时前
# LangGraph Memory机制深度解析:短期记忆与长期记忆的工程实践
前端·人工智能·python·langchain·bootstrap
金銀銅鐵8 小时前
斐波那契数列的个位数出现的周期是多少?
python·数学
whcyhhh9 小时前
头歌实践教学平台:数据科学与大数据技术导论(十二)
大数据·开发语言·python·数据清洗
qq_316411039 小时前
专业的腹腔镜医疗器械物联网APP开发服务商
python
阿童木写作9 小时前
跨境电商翻译利器,批量图片视频翻译+智能抠图工具
python·音视频
JavaPub-rodert10 小时前
ChatGPT Windows 客户端突然打不开?提示找不到 Codex CLI,我这样解决了
windows·chatgpt
绘梨衣54710 小时前
异步任务队列-学习2
爬虫·python·学习·任务队列
用户83562907805111 小时前
使用 Python 在 Excel 中插入 OLE 对象
后端·python
one31211 小时前
高精度MEMS电容式倾角传感器:原理、结构与Python三维可视化
开发语言·python