with open('testfile.txt', 'w') as f:
    f.write('hello world!')
try:
    PyPDF2.PdfFileReader(open('testfile.txt', 'rb'))
except PyPDF2.utils.PdfReadError:
    pass
Check whether a PDF-File is valid (Python)