Problem: I have an array of page numbers which blank pages need to be inserted or merged into the original pdf. Example) [1, 3, 5, 8, 10]. I need these pages to be blank and then the original document would increase in page numbers.
I have this Python script searching for specific text within a pdf file which signifies the end of a letter. Each letter is different in number of pages. Using PyPDF2, I have tried merge() with a single blank page pdf within the directory, insertBlankPage(), addPage(), addBlankPage. The problem I ran into was the blank pages were overwriting original pages. The first page that needed to be blank worked but the next pages were incorrect. It seemed like the blank pages were being written on top of existing pages vs being insert at the page number.
How can I insert blank pages at the page numbers listed in the array? Here is the code. The output array of pages does not need to be a string; it was converted to a string to bring into another program. If I can add blank pages using Python, the page number array won't need to be a string.
import PyPDF2, re
pdfIn = open('sample_letter.pdf', 'rb')
pdfFile = PyPDF2.PdfFileReader(pdfIn)
NumPages = pdfFile.getNumPages()
string = "Text I am searching for."
separator = ', '
mystring = ""
def end_of_letter():
pages = []
for page in range(NumPages):
pgObj = pdfFile.getPage(page)
text = pgObj.extractText()
match = re.search(string, text)
if match:
pages.append(str(page + 1))
mystring = separator.join(pages)
print(mystring)
return mystring
end_of_letter()
question from:
https://stackoverflow.com/questions/65830082/how-to-insert-blank-pages-into-a-pdf-using-pypdf2 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…