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!')
相关推荐
djjdjdjdjjdj3 分钟前
如何利用 watchEffect 实现在线人数实时统计?Socket 与响应式结合
jvm·数据库·python
啦啦啦_99995 分钟前
0. 工具使用
python
执笔画流年呀9 分钟前
计算机是如何⼯作的
linux·开发语言·python
m0_7164300711 分钟前
HTML函数能否用触控板高效编写_触控硬件操作体验评估【汇总】
jvm·数据库·python
2401_8359568111 分钟前
Golang怎么安全关闭channel_Golang channel关闭教程【通俗】
jvm·数据库·python
Absurd58715 分钟前
golang如何实现MQTT主题通配符路由_golang MQTT主题通配符路由实现策略
jvm·数据库·python
m0_6742946416 分钟前
宝塔面板如何设置网站强制HTTPS_配置Nginx自动跳转规则
jvm·数据库·python
qq_4240985616 分钟前
HTML函数开发用可拆卸键盘设计实用吗_模块化硬件体验评估【指南】
jvm·数据库·python
Wyz2012102417 分钟前
CSS如何实现Less颜色函数自动计算渐变_使用lighten与darken实现视觉反馈
jvm·数据库·python
weixin_4585801217 分钟前
CSS如何通过Emotion管理样式加载顺序_处理组件优先级问题
jvm·数据库·python