Using Python to split and merge pdf

Data processing and analysis 2021-04-07 18:05:03
using python split merge pdf


Knowledge point

Use Python operation PDF!

The main contents are :1、PDF Split ;2、PDF Merge .

At work , It's hard to avoid a conflict PDF Dealing with , So master a little bit of handling PDF It's very necessary to have a good skill , This article will introduce several common functions .

PDF Split

A lot of times , Acquired PDF For a long , If we want to intercept some of these pages, what should we do ? There are many tools that can do similar things , We use it Python You can do the same thing . And use Python To do something like that , It's very convenient for us to do some batch tools later .

Go straight to the code !

from PyPDF2 import PdfFileWriter, PdfFileReader
def pdf_split(pdf_in,pdf_out,start,end):
# Initialize a pdf
output = PdfFileWriter()
# Read pdf
with open(pdf_in,'rb') as in_pdf:
pdf_file = PdfFileReader(in_pdf)
# from pdf Take out the specified page from
for i in range(start, end):
output.addPage(pdf_file.getPage(i))
# Write pdf
with open(pdf_out,'ab') as out_pdf:
output.write(out_pdf)
if __name__ == '__main__':
pdf_in = ' To be divided pdf'
pdf_out = ' After split pdf'
s,e = The start page , End page
pdf_manage(pi, po, s, e)

PDF Merge

And pdf Split the opposite , yes pdf The merger of . Use Python It can also be done easily , Don't late , Don't talk nonsense , Let's go straight to the code !

from PyPDF2 import PdfFileReader,PdfFileMerger
def pdf_merger(in_pdfs,out_pdf):
# initialization
merger = PdfFileMerger()
# loop , Merge
for in_pdf in in_pdfs:
with open(in_pdf,'rb') as pdf:
merger.append(PdfFileReader(pdf))
merger.write(out_pdf)
if __name__ == '__main__':
in_pdfs = [' Put what you want to merge PDF File name , Order of attention ']
out_pdf = ' The output file '
pdf_merger(in_pdfs, out_pdf)

This article is from WeChat official account. - Data processing and analysis (Qing_master) , author : Wendao Mugong

The source and reprint of the original text are detailed in the text , If there is any infringement , Please contact the yunjia_community@tencent.com Delete .

Original publication time : 2021-03-31

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

版权声明
本文为[Data processing and analysis]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/04/20210407180223403o.html

  1. Python web menu project takes another step forward to learn about the built-in user authentication system from the application layer
  2. Python classic interview questions (with answers)!
  3. 【Python从零到壹】Python的循环结构详解
  4. 【Python从零到壹】Python列表详解
  5. 【Python从零到壹】Python的字典详解
  6. 【Python从零到壹】Python的字符串详解
  7. 【Python从零到壹】Python基础之函数的应用
  8. 【Python从零到壹】用Python实现植物大战僵尸里的面向对象
  9. Detailed explanation of Python loop structure
  10. Detailed explanation of Python list
  11. Detailed explanation of Python dictionary
  12. Detailed explanation of Python string
  13. [Python from zero to one] the application of Python basic functions
  14. [Python from zero to one] using Python to realize object-oriented in plant vs. zombie
  15. 用 Python 实现微信版飞机大战
  16. 用 Python 实现***帝国中的数字雨落既视感
  17. 想知道未来孩子长相?Python人脸融合告诉你
  18. 我用 Python 做了一个全球疫情数据大屏
  19. Using Python to realize wechat aircraft war
  20. Using Python to realize the visual sense of digital rain in the Empire of the Communist Party of China
  21. Want to know what kids will look like in the future? Python face fusion tells you
  22. I made a big screen of global epidemic data with Python
  23. python你TM太皮了——区区30行代码就能记录键盘的一举一动
  24. Python you TM too skinny - just 30 lines of code can record every move of the keyboard
  25. python的装饰器概念学习基础基础版
  26. Python decorator concept learning basic edition
  27. SQL配合Python-Flask的中转注入
  28. python3使用kivy生成安卓程序
  29. 不到 150 行代码写一个 Python 版的贪吃蛇
  30. Transfer injection of SQL and python flash
  31. Using Kivy to generate Android program in Python 3
  32. Less than 150 lines of code to write a python version of the snake
  33. Python面向对象练习题
  34. Python数据分析入门(八):Pandas统计计算和描述
  35. Python面向对象练习题
  36. Python object oriented exercises
  37. Introduction to Python data analysis (8): Pandas statistical calculation and description
  38. Python object oriented exercises
  39. WEB4-通过python获得flag
  40. python-web5
  41. Pandas-二进制操作
  42. python入门教程14-01 (python语法入门之python内存泄露)
  43. Web4 - get flag through Python
  44. python-web5
  45. Pandas binary operation
  46. python入门教程13-06 (python语法入门之视图、触发器、事务、存储过程、函数)
  47. python入门教程13-07 (python语法入门之ORM框架SQLAlchemy)
  48. python入门教程13-08 (python语法入门之python索引原理与慢查询优化)
  49. 定投指数到底能不能赚钱?Python 来告诉你答案
  50. Python入门学习之:10分钟1500访问量
  51. Getting started with Python 14-01
  52. 用 Python 画哆啦 A 梦
  53. Python 图表利器 pyecharts
  54. 用 Python 抓取公号文章保存成 HTML
  55. Introduction to Python 13-06 (view, trigger, transaction, stored procedure, function of introduction to Python syntax)
  56. Getting started with Python 13-07 (ORM framework Sqlalchemy for getting started with Python syntax)
  57. Introduction to Python 13-08
  58. Can fixed investment index make money? Python will tell you the answer
  59. Introduction to Python: 1500 visits in 10 minutes
  60. 用 Python 获取股市交易数据