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!')
相关推荐
小小西贝20 分钟前
pycharm无法正常调试问题
ide·python·pycharm
百锦再1 小时前
Vue环境下数据导出PDF的全面指南
前端·javascript·vue.js·python·django·pdf·pygame
IDRSolutions_CN1 小时前
如何在Java中处理PDF文档(教程)
java·经验分享·pdf·软件工程·团队开发
拓端研究室1 小时前
2025年度消费新潜力白皮书470+份汇总解读|附PDF下载
大数据·人工智能·pdf
Sandman6z3 小时前
uv python 卸载
开发语言·python·uv
三道杠卷胡4 小时前
【AI News | 20250521】每日AI进展
人工智能·python·计算机视觉·语言模型·aigc
Chocolate_men4 小时前
ftp、http下载远程文件(多线程、断点续传)
python·网络协议·http
CodeCraft Studio5 小时前
PDF处理控件Aspose.PDF教程:以编程方式将PDF转换为Word
pdf·c#·word
测试19985 小时前
Selenium无法定位元素的几种解决方案详解
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
花菜会噎住6 小时前
Python 计算机网络TCP网络应用程序开发
网络·python·tcp/ip·计算机网络·客户端·服务端