当前位置:网站首页>【office办公-pdf篇】pdf合并与拆分让我们摆脱付费软件的功能限制好不好
【office办公-pdf篇】pdf合并与拆分让我们摆脱付费软件的功能限制好不好
2022-07-01 00:41:00 【德天老师】
好工具,可以事半功倍,今天一起学习Pdf合与分
工具模块PyPDF2
层次分析—文件路径与文件读取与保存
读入文件夹:
data\in
读入文件名:temp.pdf
保存文件夹:
data\out
保存文件,1.pdf,2.pdf,3.pdf…
合并文件:
431.pdf
层次分析—从文件中取出每一页
pdf_writer = PdfFileWriter()
page = pdfReader.getPage(page_no)
pdf_writer.addPage(page)
层次分析—从文件中取出每一页
构造出包括路径和每一页文件名的路径信息
file ='{0}.pdf'.format(page_no + 1)
outfile = os.path.join(outdir,file)
层次分析—创建路径信息到硬盘,同时调用pdf_writer中的保存的页面信息,写入到指定outfile位置
注:output_pdf是接收来自pdf_writer的写入信息的。
with open(outfile,'wb') as output_pdf:
pdf_writer.write(output_pdf)
print('拆分并把文件写入')
层次分析—重新建立写变量pdf_wrigter,指定序号为3,0,2页面添加到pdf_writer中(合并)
pdf_writer = PdfFileWriter()
pdf_writer.addPage(pdfReader.getPage(3))
pdf_writer.addPage(pdfReader.getPage(0))
pdf_writer.addPage(pdfReader.getPage(2))
层次分析—重新建立文件名,路径,完成文件信息,通过with open()写入硬盘,完成合并后的写入操作
file = '431.pdf'
outfile = os.path.join(outdir,file)
with open(outfile,'wb') as output_pdf:
pdf_writer.write(output_pdf)
print('选择需要页合并')
以下为完整的代码
# coding = utf-8
from PyPDF2 import *
import os
indir = r'data\in'
outdir = r'data\out'
infile = os.path.join(indir,'temp.pdf') #读取文件路径
pdfReader = PdfFileReader(infile)
for page_no in range(pdfReader.numPages):
pdf_writer = PdfFileWriter()
page = pdfReader.getPage(page_no)
pdf_writer.addPage(page)
file ='{0}.pdf'.format(page_no + 1)
outfile = os.path.join(outdir,file)
with open(outfile,'wb') as output_pdf:
pdf_writer.write(output_pdf)
print('拆分完成')
pdf_writer = PdfFileWriter()
pdf_writer.addPage(pdfReader.getPage(3))
pdf_writer.addPage(pdfReader.getPage(0))
pdf_writer.addPage(pdfReader.getPage(2))
file = '431.pdf'
outfile = os.path.join(outdir,file)
with open(outfile,'wb') as output_pdf:
pdf_writer.write(output_pdf)
print('选择需要页合并')
读后感:
- 代码分段书写,分析,目的是方便读者阅读理解每小段代码的内涵
- 通过解析这些小的代码片断,让大家理解代码运行的逻辑
- 通过前后的分析让大家知道python代码还是很容易理解,很容易阅读的
学习体会:
语言的学习不是一个快速的过程,能力的提升,确实需要知识的累积,每一个小的代码的内涵,组合成大段的代码块,大的代码块组合成整个软件体,从而完成系统性工程的创建。
提示每一个学习者不要过于心急,在自己的时间允许范围内,让真敲代码,仔细琢磨代码运行的逻辑,积累多了,你终于在某一天,也会突然感到豁然开朗。
边栏推荐
猜你喜欢

2021电赛F题openmv和K210调用openmv api巡线,完全开源。
![[network packet loss and network delay? This artifact can help you deal with everything!]](/img/c4/f733b23327458b9266b9cbcccb6f14.png)
[network packet loss and network delay? This artifact can help you deal with everything!]

Gavin's insight on the transformer live broadcast course - rasa project's actual banking financial BOT Intelligent Business Dialogue robot system startup, language understanding, dialogue decision-mak

Installing mongodb database in Windows Environment
![[问题已处理]-nvidia-smi命令获取不到自身容器的GPU进程和外部的GPU进程号](/img/51/e48e222c14f4a4e9f2be91a677033f.png)
[问题已处理]-nvidia-smi命令获取不到自身容器的GPU进程和外部的GPU进程号

Practical shell knowledge

uniapp官方组件点击item无效,解决方案
![Parity linked list [two general directions of linked list operation]](/img/4e/ce860bc172bb75f456427ba26a7842.png)
Parity linked list [two general directions of linked list operation]

Pre training / transfer learning of models

解析融合学科本质的创客教育路径
随机推荐
pytorch编程知识(2)
Installing mongodb database in Windows Environment
Windows环境下安装MongoDB数据库
Practical shell knowledge
Split the linked list [take next first and then cut the linked list to prevent chain breakage]
Principes de formation de la programmation robotique
生意和投资的思考
Web compatibility testing of software testing
Looksrare team's "cash out" caused disturbance
Impact relay zc-23/dc220v
Analyzing the wisdom principle in maker education practice
Basic knowledge of software and hardware -- diary (1)
个人博客搭建与美化
Solve idea:class' xxx 'not found in module' xxx‘
Dls-20 double position relay 220VDC
Share your own terminal DIY display banner
Orb-slam2 source code learning (II) map initialization
Metauniverse and virtual reality (II)
Tcp/ip protocol stack, about TCP_ RST | TCP_ ACK correct attitude
Service grid ASM year end summary: how do end users use the service grid?