ValueError: incomplete format
Question:
I’m trying to take two PDF documents, remove the first page from doc1 and add page 1 from doc2. I think I have the code mostly where it needs to be, but I’m receiving “ValueError: incomplete format.” Here is the code I have, and the error it’s giving me is for the third to last line.
import os, shutil, sys, PyPDF2
from PyPDF2 import PdfFileWriter, PdfFileReader
origList = list()
for root, dirs, files in os.walk(r'C:UsersUser1Desktoppytestorig'):
for file in files:
origList.append(file)
print "Original PDF list:"
print origList
stampList = list()
for root, dirs, files in os.walk(r'C:UsersUser1Desktoppyteststamp'):
for file in files:
stampList.append(file)
print "Stamped PDF List:"
print stampList
oX = 0
output = PdfFileWriter()
for document in origList:
file1 = PdfFileReader(open(r'C:UsersUser1Desktoppytestorig%s' %origList[oX], "rb"))
file2 = PdfFileReader(open(r'C:UsersUser1Desktoppyteststamp%s' %stampList[oX], "rb"))
file1.decrypt("")
curFile = origList[oX]
output.addPage(file2.getPage(0))
file2Pages = file2.getNumPages()
file2Counter = 1
while file2Counter <= file2Pages:
output.addPage(file1.getPage(file2Counter))
file2Counter = file2Counter + 1
outputStream = file(r'C:UsersUser1Desktoppytestoutput%' %curFile, "wb")
output.write(outputStream)
oX = oX + 1
The line the error is for is:
outputStream = file(r'C:UsersUser1Desktoppytestoutput%' %curFile, "wb")
I followed the examples given with the module, and I thought that was how it should be written.
Answers:
On the line that gives the error, you’re missing the format specifier, i.e.
outputStream = file(r'C:UsersUser1Desktoppytestoutput%' %curFile, "wb")
^
# what type are we formatting? Need to add an 's' here for 'string'
Compare to a previous line…
file1 = PdfFileReader(open(r'C:UsersUser1Desktoppytestorig%s' %origList[oX], "rb"))
^
# the output line is missing this 's'
I’m trying to take two PDF documents, remove the first page from doc1 and add page 1 from doc2. I think I have the code mostly where it needs to be, but I’m receiving “ValueError: incomplete format.” Here is the code I have, and the error it’s giving me is for the third to last line.
import os, shutil, sys, PyPDF2
from PyPDF2 import PdfFileWriter, PdfFileReader
origList = list()
for root, dirs, files in os.walk(r'C:UsersUser1Desktoppytestorig'):
for file in files:
origList.append(file)
print "Original PDF list:"
print origList
stampList = list()
for root, dirs, files in os.walk(r'C:UsersUser1Desktoppyteststamp'):
for file in files:
stampList.append(file)
print "Stamped PDF List:"
print stampList
oX = 0
output = PdfFileWriter()
for document in origList:
file1 = PdfFileReader(open(r'C:UsersUser1Desktoppytestorig%s' %origList[oX], "rb"))
file2 = PdfFileReader(open(r'C:UsersUser1Desktoppyteststamp%s' %stampList[oX], "rb"))
file1.decrypt("")
curFile = origList[oX]
output.addPage(file2.getPage(0))
file2Pages = file2.getNumPages()
file2Counter = 1
while file2Counter <= file2Pages:
output.addPage(file1.getPage(file2Counter))
file2Counter = file2Counter + 1
outputStream = file(r'C:UsersUser1Desktoppytestoutput%' %curFile, "wb")
output.write(outputStream)
oX = oX + 1
The line the error is for is:
outputStream = file(r'C:UsersUser1Desktoppytestoutput%' %curFile, "wb")
I followed the examples given with the module, and I thought that was how it should be written.
On the line that gives the error, you’re missing the format specifier, i.e.
outputStream = file(r'C:UsersUser1Desktoppytestoutput%' %curFile, "wb")
^
# what type are we formatting? Need to add an 's' here for 'string'
Compare to a previous line…
file1 = PdfFileReader(open(r'C:UsersUser1Desktoppytestorig%s' %origList[oX], "rb"))
^
# the output line is missing this 's'