コード例 #1
0
ファイル: read.py プロジェクト: sfneal/pdfconduit
def pypdf3_reader(pdf, decrypt=None):
    """
    Retrieve a PdfFileReader object that has been decrypted if a password is specified.

    :param pdf: PDF document to read
    :param decrypt: Owner password to decrypt pdf
    :return: PdfFileReader object
    """
    if decrypt:
        reader = PdfFileReader(pdf)
        reader.decrypt(decrypt)
        return reader
    else:
        return PdfFileReader(pdf)
コード例 #2
0
 def _reader(path, password, prompt):
     """Read PDF and decrypt if encrypted."""
     pdf = PdfFileReader(path) if not isinstance(path, PdfFileReader) else path
     # Check that PDF is encrypted
     if pdf.isEncrypted:
         # Check that password is none
         if not password:
             pdf.decrypt('')
             # Try and decrypt PDF using no password, prompt for password
             if pdf.isEncrypted and prompt:
                 print('No password has been given for encrypted PDF ', path)
                 password = input('Enter Password: ')
             else:
                 return False
         pdf.decrypt(password)
     return pdf
コード例 #3
0
def get_reader(filename, password):
    global old_file
    try:
        old_file = open(filename, 'rb')
    except Exception as err:
        print('文件打开失败!' + str(err))
        write_result(filename, '文件打开失败')
        return None

    # 创建读实例

    pdf_reader = PdfFileReader(old_file, strict=False)
    # 解密操作
    if pdf_reader.isEncrypted:
        if password is None:
            print('%s文件被加密,需要密码!' % filename)
            write_result(filename, '文件需要密码')
            return None
        else:
            if pdf_reader.decrypt(password) != 1:
                print('%s密码不正确!' % filename)
                return None
    # if 'old_file' in locals(): # 这句话需要使用字符串格式,否则无法关闭文件
    #     old_file.close()
    return pdf_reader
コード例 #4
0
def pdf_parser(s):
    s = s.strip()
    # required to suppress warning messages
    with open(os.devnull, 'w') as fp:
        pdf = PdfFileReader(BytesIO(s), strict=False, warndest=fp)
    if pdf.isEncrypted:
        try:
            pdf.decrypt('')
        except NotImplementedError:
            return {}
    meta = pdf.getDocumentInfo() or {}
    #print(str(meta))
    result = {}
    for key in meta.keys():
        result[key[1:]] = meta.get(key)
    return result
コード例 #5
0
def get_reader(filename, password):
    try:
        old_file = open(filename, 'rb')
    except Exception as err:
        print('文件打开失败!' + str(err))
        return None

    # 创建读实例
    pdf_reader = PdfFileReader(old_file, strict=False)

    # 解密操作
    if pdf_reader.isEncrypted:
        if password is None:
            print('%s文件被加密,需要密码!' % filename)
            return None
        else:
            if pdf_reader.decrypt(password) != 1:
                print('%s密码不正确!' % filename)
                return None
    if old_file in locals():
        old_file.close()
    return pdf_reader