1

I have an application that converts documents(tif, docx, xlsx, etc) to pdf files. After the documents are converted they are exported to other applications (DMS, Kofax, SharePoint, etc). During export sometimes the documents generate an error (file already open by an other application). How can i check the state of an PDF document with python. I ma trying some basic stuff here to see how i can write a test application (python) to check the documents that have generated an error. Below the simple code i test with. But if i open de pdf in a PDF Reader an then run the code the PDF opens without an error. Any advice on this. How can I check if a PDF file is already opened?

import PyPDF2
try:
    pdf = PyPDF2.PdfFileReader(open("test.pdf", "rb"))

except PyPDF2.utils.PdfReadError:
    print("invalid PDF file")
else:
    print("Valid PDF!")
    print(pdf.getDocumentInfo())

1 Answers1

0

Try this

import os

f = 'C:/test.pdf'
if os.path.exists(f):
  try:
     os.rename(f, f)
     print 'Yo!! you can mess with it'
exce pt OSError as e:
    print 'Access-error on file "' + f + '"! \n' + str(e)
Muhammad Usman
  • 10,039
  • 22
  • 39
  • Thanks for the reply I have tried it but it doesn't seem to work. If I use the rename option the PDF gets corrupted. I have played around with os.open but when the PDF is already opened the code just runs fine. I also have looked at the duplicate thread and i'll have a look at the psutil package. That sounds like the one that could resolve the problem. – Eric Feurich Oct 25 '17 at 15:02