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!')
相关推荐
2601_9628857212 分钟前
baostock vs Tushare:哪个免费股票数据 API 更好用?
python
hahaha601617 分钟前
数字图像锐化
开发语言·python
xiangjiaodalishi25 分钟前
主流 AI 爬虫 User-Agent 清单大全:分组、配置与持续维护(2026 版)
人工智能·爬虫·python
普贤莲花27 分钟前
先学Workbuddy还是“人工智能训练师”,或者python基础课?
开发语言·人工智能·python
砚底藏山河27 分钟前
python量化实操:回测数据缓存与重放
java·python·金融·1024程序员节
第404号工位1 小时前
浏览器的“无痕模式”是当代最大安慰剂?我用 30 行 JS 提取硬件指纹,跨网络换代理照样精准锁定你
linux·人工智能·python
小熊吃保安1 小时前
Windows也能用 Apple Watch 解锁
windows·rust·开源
凌界天行1 小时前
知名的无人机侦测肩灯制造厂名声
python
XZ-0700011 小时前
week10-直方图
python
外收内放2 小时前
FastAPI猜字谜实战(6):日志与异常
开发语言·python·学习·fastapi