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 .
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)
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 email@example.com Delete .
Original publication time ： 2021-03-31
Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .