尝试使用python解密(pypdf2)文件对象时出现UnicodeEncodeError

2024-04-19 12:40:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我在python中遇到了以下错误

UnicodeEncodeError: 'latin-1' codec can't encode character '\u018f' in position 0: ordinal not in range(256)

在试图使用pypdf2itertools库文件强制破解加密的pdf时。你知道吗

这是我的密码:

enter image description here

奇怪的是,代码在使用不同的机器时运行良好…python版本是3.6.4,我的和我试过的机器都是。你知道吗

我试过的是:

  • 我试着在另一台机器上运行代码,效果很好。这让我觉得我没有正确安装python
  • 我刚安装了pythonv3.6.4和pycharm(最新社区版本)。失败后,我尝试了anaconda(最新版本),但也失败了,于是我又回到了pythonv3.6.4

        import PyPDF2 as pdf
        import itertools
    
        testFile = pdf.PdfFileReader("File01_Enc.pdf")
        print(testFile.isEncrypted)
    
        # for n in range(100001):
        #     if n <= 100:
        #         print(n)
        #         t = str(n)
        #         x = testFile.decrypt(t)
        #         if x == 2 or x == 1:
        #             print("The password is: " + str(t)
        #             break
        pwds = itertools.combinations("0123456789", 6)
        for p in pwds:
            print(''.join(p))
            x = testFile.decrypt(''.join(str(p)))
            if x > 0:
                print("The password is: " + ''.join(str(p)))
                break
    

Tags: 代码inimport版本机器forifpdf