Print

Print


Good morning everyone,

Back again with another Python question. I am using the module PyPDF2 to manipulate PDFs. You can add pages, rotate them, merge, or split PDF documents. You can also compress them, something I'd like to leverage as our program has over 17,000 PDFs on our network.

I am working with the PyPDF 2module and trying a simple rotate without success. I would like to open a given document, rotate it, and then save it as the same document. Whether I use the variable "fullPath" or a literal string with the path, I keep getting the error invalid argument and I can't figure out why. If I can do this, I think I could also figure out the compress function PyPDF2 offers as well.

--------------------------------------
Here is my code:
--------------------------------------
import os
from PyPDF2 import PdfFileWriter, PdfFileReader

path = r"H:\Documents\Compression_Test_Folder"


for root, dirs, files in os.walk(path):
    for fileName in files:
        fullPath = os.path.join(root, fileName)
        basename, extension = os.path.splitext(fullPath)
        if extension == ".pdf":
            output = PdfFileWriter()
            inputStream = open(fullPath, "rb")
            input1 = PdfFileReader(inputStream)

            output.addPage(input1.getPage(0).rotateClockwise(90))

            del input1

            outputStream = file(fullPath, "wb")
            output.write(outputStream)
-------------------------------------------------
And here is the error message result:
--------------------------------------------------

Traceback (most recent call last):
  File "I:\ARC\Data\Expressions\Clean_CountPDFPage.py", line 14, in <module>
    input1 = PdfFileReader(inputStream)
  File "C:\Python27\ArcGIS10.1\lib\site-packages\PyPDF2\pdf.py", line 831, in __init__
    self.read(stream)
  File "C:\Python27\ArcGIS10.1\lib\site-packages\PyPDF2\pdf.py", line 1279, in read
    stream.seek(-1, 2)
IOError: [Errno 22] Invalid argument


Any thoughts?


Kind regards,

Michael Lachance
Plant Protection Technician
ALB Eradication Program USDA APHIS
151 West Boylston Drive
Worcester, MA 01606
508.852.8050 (o)
508.414.5673 (c)
The USDA is an equal opportunity provider and employer.
Federal Relay Service (Voice/TTY/ASCII/Spanish) 1-800-877-8339

This electronic message contains information generated by the USDA solely for the intended recipients. Any unauthorized interception of this message or the use or disclosure of the information it contains may violate the law and subject the violator to civil or criminal penalties. If you believe you have received this message in error, please notify the sender and delete the email immediately.


------------------------------------------------------------------------- This list (NEARC-L) is an unmoderated discussion list for all NEARC Users.

If you no longer wish to receive e-mail from this list, you can remove yourself by going to http://listserv.uconn.edu/nearc-l.html.