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!')
相关推荐
私人珍藏库9 分钟前
[Windows] Mouser v3.5.3第三方罗技鼠标驱动
windows·计算机外设·工具·软件·win·多功能
m0_734949796 小时前
MySQL如何配置定时清理过期备份文件_find命令与保留周期策略
jvm·数据库·python
m0_514520577 小时前
MySQL索引优化后性能没提升_通过EXPLAIN查看索引命中率
jvm·数据库·python
H Journey7 小时前
Python 国内pip install 安装缓慢
python·pip·install 加速
Polar__Star8 小时前
如何在 AWS Lambda 中正确使用临时凭证生成 S3 预签名 URL
jvm·数据库·python
m0_743623928 小时前
React 自定义 Hook 的命名规范与调用规则详解
jvm·数据库·python
FreakStudio9 小时前
无硬件学LVGL—定时器篇:基于Web模拟器+MicroPython速通GUI开发
python·单片机·嵌入式·大学生·面向对象·并行计算·电子diy·电子计算机
сокол9 小时前
【网安-应急响应-基础记录】Windows入侵排查
windows·web安全·网络安全·系统安全
Bruce_Liuxiaowei9 小时前
Windows防火墙规则导出工具:让安全配置可备份、可迁移、可审计
运维·windows·安全·网络安全
gCode Teacher 格码致知10 小时前
Python提高:pytest的简单案例-由Deepseek产生
python·pytest