# -*- coding: utf-8 -*-
"""
Created on Thu May 11 21:08:06 2023
@author: xianglong
"""
#奇数页PDF插入空白页
import os,PyPDF2,pyperclip
pathofcwd = r'./讲义/'
# ^ 需要处理的pdf存放位置
class pdfReader:
# ^ 处理pdf的一个类,把和pdf处理有关的代码都放在这里了
blankPdfPath = r'D:/学习/CPA/空白.pdf'
# ^ 空白页pdf存放位置
def __init__(self,pdfPath):
self.pdfPath = pdfPath
self.blankPageFile, self.blankPage = self.openAndReadit(self.blankPdfPath)
self.pdfFile, self.pdfReader = self.openAndReadit(self.pdfPath)
def openAndReadit(self,pdfpath):
"""
generate the pdfReader object for given path in parameter
"""
pdfFile = open(pdfpath, 'rb')
pdfReader = PyPDF2.PdfReader(pdfFile)
return (pdfFile,pdfReader)
def appendBlank(self):
"""
no para, return a pdf writer with blankPage appended
"""
pdfWriter = PyPDF2.PdfWriter()
for pageNum in range(len(self.pdfReader.pages)):
pageObj = self.pdfReader.pages[pageNum]
print(pageObj)
pdfWriter.add_page(pageObj)
# add the blank page:
pdfWriter.add_page(self.blankPage.pages[0])
return pdfWriter
def closeAllFile(self):
self.blankPageFile.close()
self.pdfFile.close()
os.chdir(pathofcwd)
fileList = os.listdir()
pdfList = filter(
lambda e:os.path.splitext(e)[1]=='.pdf',
fileList
)
# ^ 过滤文件列表,只保留pdf
pdfReaderList = map(
lambda e:pdfReader(e),
pdfList
)
# ^ 根据pdf文件路径生成pdfReader类
pdfReaderList = filter(
lambda e: len(e.pdfReader.pages) % 2 == 1,
pdfReaderList
)
# ^ 只保留奇数页pdf的pdfReader类
pdfReaderList = list(pdfReaderList)
for pdfReader in pdfReaderList:
pdfAddBlankWriter = pdfReader.appendBlank()
outputPath = os.path.splitext(pdfReader.pdfPath)[0]+'_addBlank'+'.pdf'
pdfOutputFile = open(outputPath,'wb')
pdfAddBlankWriter.write(pdfOutputFile)
pdfOutputFile.close()
pdfReader.closeAllFile()
print("preparing to output as:%s" % outputPath)
pdf奇数插入空白页
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...