from PyPDF2 import PdfFileReader, PdfFileWriter
def split(source, resultName, cutpage):
pdf = PdfFileReader(source)
pdf_writer = PdfFileWriter()
for page in range(0, pdf.getNumPages()):
if (page + 1 not in cutpage):
pdf_writer.addPage(pdf.getPage(page))
output = f'{resultName}.pdf'
with open(output, 'wb') as output_pdf:
pdf_writer.write(output_pdf)
if __name__ == '__main__':
path = '毕业设计.pdf'
split(path, '毕业设计fk', [2])
三个参数分别是源文件路径及文件名,输出路径及文件名,删除的页码,从1开始","隔开,如[2,4,5]
def plus(sources, resultName):
pdf_writer = PdfFileWriter()
for path in sources:
pdf = PdfFileReader(path)
for page in range(0, pdf.getNumPages()):
pdf_writer.addPage(pdf.getPage(page))
output = f'{resultName}.pdf'
with open(output, 'wb') as output_pdf:
pdf_writer.write(output_pdf)
if __name__ == '__main__':
paths = ['英文原文.pdf', '英文翻译.pdf']
plus(paths, '外文翻译.pdf')
合并多个pdf。