繁体   English   中英

如何使用python在pdf文件末尾附加新的空白页

[英]how to append new blank page at the end of pdf file using python

我正在使用PyPDF2 lib,但它只会覆盖空白页面,并且不会在PDF文件的末尾附加。 这是我的代码。

#this is the file i'm creating
file1 = canvas.Canvas("Statements.pdf", pagesize=letter)
file1.drawString(100,400,"HELLOOOO")
file1.save()

# using this code i want to append blank page at the end but it overwrites 
#with blank page
with open("Statements.pdf", 'rb') as input:
pdf=PdfFileReader(input)
numPages=pdf.getNumPages()

outPdf=PdfFileWriter()
outPdf.cloneDocumentFromReader(pdf)
outPdf.addBlankPage()
outStream=file('Statements.pdf','wb')
outPdf.write(outStream)
outStream.close()

cloneDocumentFromReader似乎有问题,请参见: https : //github.com/mstamy2/PyPDF2/issues/219
如果您不尝试添加空白页而是仅执行克隆,则最终将得到一个空文件。

SPYBUG96引用的以下内容适用于我(Linux)

import PyPDF2
import shutil
a = open("some_pdf_file.pdf", 'rb')
pdf=PyPDF2.PdfFileReader(a)
numPages=pdf.getNumPages()
outPdf=PyPDF2.PdfFileWriter()
outPdf.appendPagesFromReader(pdf)
#outPdf.cloneDocumentFromReader(pdf)
outPdf.addBlankPage()
outStream=open('Amended.pdf','wb')
outPdf.write(outStream)
outStream.close()
a.close()
#Copy amended file back over the original
shutil.copyfile('Amended.pdf','some_pdf_file.pdf')

注意:您可以使用shutil.move('Amended.pdf','some_pdf_file.pdf')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM