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!')
相关推荐
渣渣xiong18 分钟前
从零开始:前端转型AI agent直到就业第五十七天-第五十八天
前端·人工智能·python
小L~~~1 小时前
基于贪心策略的混合遗传算法求解01背包问题
python·算法
才兄说1 小时前
机器人二次开发机器人动作定制?动作迁移数据优化
python
用户8356290780512 小时前
用 Python 实现 Excel 散点图绘制与定制
后端·python
PAK向日葵2 小时前
从零实现 Python 虚拟机(一):PVM 基本原理介绍
python
神所夸赞的夏天2 小时前
创建虚拟环境提示SSLError错误
python
极光代码工作室2 小时前
基于机器学习的二手商品价格预测系统
人工智能·python·深度学习·机器学习
无情的西瓜皮2 小时前
MCP协议实战:从零搭建一个AI Agent工具服务器
运维·服务器·python
fengchengwu20122 小时前
Jupyter 安装与使用指南:从环境配置到效率翻倍
ide·windows·jupyter
IT策士2 小时前
Django 从 0 到 1 打造完整电商平台:系列总结 + 项目演示与后续扩展
后端·python·django