【问题标题】:Load xls files with pandas is failed使用 pandas 加载 xls 文件失败
【发布时间】:2021-09-16 06:22:03
【问题描述】:

我正在尝试使用 pandas 加载 xls 文件:

pd.read_excel(fi_name, sheet_name=None, engine=None)

但我得到这个错误:

"XLRDError: Workbook is encrypted"

但是文件没有加密,我可以用excel打开它,用tika包读取文件的文本。

有人知道我该如何解决吗?

此外,有谁知道用于读取所有 excel 文件格式的 python 包, 就算熊猫失败了?

谢谢

【问题讨论】:

    标签: python excel pandas xlrd apache-tika


    【解决方案1】:

    这有两个可能的原因:

    1. 您获取的文件与文件扩展名显示的文件格式不同。

    2. 整个工作簿或其中的一张工作簿都受密码保护,因此从中读取的数据被加密以保护数据。

    【讨论】:

    • 1.我用文件的字节加载熊猫,而不是文件路径。 2.文件没有加密,可以打开excel没有任何问题。
    • 那么在这种情况下,您可能能够在 Excel 中打开文件,但是在 Excel 中打开文件时您不需要提供默认密码“VelvetSweatshop”,但是在数据框中读取文件时需要。如果是这种情况,您可以使用提供的第二种解决方案。如果这也不能解决您的问题,请在此处提供您的文件和代码。一定会尽力帮助你!
    【解决方案2】:

    我想,我找到了解决您问题的方法:

    import msoffcrypto
    
    file = msoffcrypto.OfficeFile (open ('encrypted.xls', 'rb')) # read the original file
    file.load_key (password = 'VelvetSweatshop') # Fill in the password, if it can be opened directly, the default password is 'VelvetSweatshop'
    file.decrypt (open ('decrypted.xls', 'wb')) # Save it as a new file after decryption
    

    之后就可以使用xlrd正常打开和操作解密后的文件了。

    你可以安装包

    pip install msoffcrypto
    

    你可以看到完整的文档here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-23
      • 1970-01-01
      • 2016-02-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-03
      • 2012-04-21
      相关资源
      最近更新 更多