日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

使用pyPdf分割pdf文档

發(fā)布時(shí)間:2023/12/20 编程问答 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用pyPdf分割pdf文档 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

d

# -*- coding: utf-8 -*-from PyPDF2 import PdfFileReader, PdfFileWriterimport sys,getopt,ostry:opts,args=getopt.getopt(sys.argv[1:],"i:o:p:",["page="]) except getopt.GetoptError:print 'arg error'sys.exit(1)inFile='' outFile='' page=10print '開始分割' print '輸入文件',inFile print '輸出文件',outFile print '每個(gè)文檔頁(yè)數(shù)',pagefor opt,arg in opts:if opt == '-i':inFile=argelif opt =='-o':outFile=argelif opt in ['-p','--page']:page=max(page,int(arg))def split_pdf(infn,outfn,page):pdfInput = PdfFileReader(open(infn, 'rb'))inputPageCount = pdfInput.getNumPages()pdfOutput=NoneoutputFile=NonecurrentCount=0if os.path.exists(outfn) and os.path.isdir(outfn):print outfn+'目錄已經(jīng)存在,直接使用該目錄'else :print outfn+'目錄不存在,正創(chuàng)建目錄'os.mkdir(outfn)for i in range (0, inputPageCount):if pdfOutput==None:pdfOutput=PdfFileWriter()outName=outfn+"/"+outfn+str(currentCount)+".pdf"outputFile=open(outName,"wb")print '開始處理第',currentCount,'個(gè)文檔'if i+1==inputPageCount or (i+1)%page==0:pdfOutput.write(outputFile)outputFile.close()pdfOutput=NoneoutputFile=NonecurrentCount+=1else:pdfOutput.addPage(pdfInput.getPage(i))print '分割',inFile,'成功'print '輸出目錄為', outfnprint '輸出文檔共',currentCount,'個(gè)'split_pdf(inFile,outFile,page)def merge_pdf(infnList, outfn):pdf_output = PdfFileWriter()for infn in infnList:pdf_input = PdfFileReader(open(infn, 'rb'))# 獲取 pdf 共用多少頁(yè)page_count = pdf_input.getNumPages()print(page_count)for i in range(page_count):pdf_output.addPage(pdf_input.getPage(i))pdf_output.write(open(outfn, 'wb'))

總結(jié)

以上是生活随笔為你收集整理的使用pyPdf分割pdf文档的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。