当前位置: 首页 > article >正文

python读取PDF文件中的指定页码的范围并存储到指定的文件名

读取PDF文件中的指定页码的范围并存储到指定的文件名

# -*- 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!')

http://www.kler.cn/a/147783.html

相关文章:

  • 一次成功流水账-RBDL库的安装与验证
  • 一键打断线(根据相交点打断)——CAD c# 二次开发
  • 06 - Django 视图view
  • vue中proxy代理配置(测试二)
  • springboot项目对数据库密码加解密
  • 服务器证书原理
  • Rust语言入门教程(七) - 所有权系统
  • 阅读笔记——《Removing RLHF Protections in GPT-4 via Fine-Tuning》
  • C# 实现微信退款及对帐
  • QT 界面切换
  • C++相关闲碎记录(1)
  • 图形编辑器开发:缩放和旋转控制点
  • base64 前端显示 data:image/jpg;base64
  • MySQL-02-InnoDB存储引擎
  • git-3
  • visual c++ 2019 redistributable package
  • screen无法翻页的问题
  • MySQL表的操作『增删改查』
  • JAXB的XmlAttribute注解
  • 【Python】Vscode解决Python中制表符和空格混用导致的缩进问题
  • 如何通过内网穿透实现公网远程ssh连接kali系统
  • 才聚免费为你招聘,用人单位看过来!
  • 011 OpenCV warpAffine
  • opencv-图像金字塔
  • dsp flash如何同时烧写boot和app
  • springboot实现验证码功能