简体   繁体   中英

Python PyPDF2 Combine Docs

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:\Users\User1\Desktop\pytest\orig'):
    for file in files:
            origList.append(file)
print "Original PDF list:"
print origList

stampList = list()
for root, dirs, files in os.walk(r'C:\Users\User1\Desktop\pytest\stamp'):
    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:\Users\User1\Desktop\pytest\orig\%s' %origList[oX], "rb"))
    file2 = PdfFileReader(open(r'C:\Users\User1\Desktop\pytest\stamp\%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:\Users\User1\Desktop\pytest\output\%' %curFile, "wb")
    output.write(outputStream)
    oX = oX + 1

The line the error is for is:

outputStream = file(r'C:\Users\User1\Desktop\pytest\output\%' %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, ie

outputStream = file(r'C:\Users\User1\Desktop\pytest\output\%' %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:\Users\User1\Desktop\pytest\orig\%s' %origList[oX], "rb"))
                                                                 ^
                              # the output line is missing this 's'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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