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!')
相关推荐
2501_915106327 分钟前
Windows 上开发 Unity iOS,使用 KXBuild 进行真机调试、性能采样与打包 ipa
windows·vscode·unity·ios·个人开发·swift·敏捷流程
执假以为真19 分钟前
Python内存分析器memray的简单介绍
python
西柚不烦恼26 分钟前
Windows 本地部署私有 AI 全攻略:纯 CPU 环境(Ollama + Open WebUI)
人工智能·windows
收纳过往29 分钟前
数据库加索引的时机:慢查询日志说话
android·开发语言·数据库·python·低代码·rxjava
少年的范儿32 分钟前
Windows 安装 Node.js 教程:nvm 版本管理 + pnpm 安装配置
windows·npm·node.js
小鹿学程序1 小时前
市赛python学习
python
高洁011 小时前
知识图谱:结构化表达实体与关系的知识组织技术
人工智能·python·深度学习·机器学习·迁移学习
骊山道童1 小时前
第 1 周 LLM 原理与高效微调知识精讲手册
python
Metaphor6922 小时前
轻松使用 Python 将 RTF 转换为 Word(含批量转换与格式互转)
python·word·rtf
zh路西法2 小时前
【深入理解 LSTM】从时间反向传播(BPTT)到长时记忆:门控机制与实验验证
pytorch·python·rnn·gru·lstm·bptt