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!')
相关推荐
小诸葛IT课堂9 分钟前
PyTorch 生态概览:为什么选择动态计算图框架?
人工智能·pytorch·python
杜子腾dd11 分钟前
16.使用读写包操作Excel文件:XlsxWriter 包
大数据·开发语言·python·excel·pandas
带娃的IT创业者13 分钟前
Flask应用调试模式下外网访问的技巧
后端·python·flask
CL_IN19 分钟前
零售业务订单处理自动化:吉客云对接金蝶云星空
windows·自动化·零售
print('name')27 分钟前
将景区天气数据存储到Excel文件中
开发语言·数据结构·python·pycharm·excel·visual studio code
愚戏师1 小时前
Python:函数式编程
开发语言·python·算法
iku!!1 小时前
【Python+HTTP接口】POST请求不同请求头构造
python·http
DogDaoDao1 小时前
Conda 虚拟环境创建:加不加 Python 版本的深度剖析
开发语言·人工智能·pytorch·python·深度学习·conda
Leo来编程1 小时前
Python学习第十九天
python·学习
如果是君2 小时前
Ubuntu20.04安装运行DynaSLAM
linux·python·深度学习·神经网络·ubuntu