【问题标题】:Pandas unable to open this Excel filePandas 无法打开此 Excel 文件
【发布时间】:2018-01-25 07:26:37
【问题描述】:

我正在尝试使用 python pandas 打开 Excel 文件。代码很简单,如下图;

import pandas as pd
df = pd.read_excel('../TestXLWings.xlsm', sheetname="TestSheet")

下面出现错误;

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2\helpers\pydev\pydevd.py", line 1599, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2\helpers\pydev\pydevd.py", line 1026, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/testing/Dropbox/Test-XLwings/test.py", line 3, in <module>
    df = pd.read_excel('../TestXLWings.xlsm', sheetname="TestSheet")
  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\excel.py", line 203, in read_excel
    io = ExcelFile(io, engine=engine)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\excel.py", line 260, in __init__
    self.book = xlrd.open_workbook(io)
  File "C:\ProgramData\Anaconda3\lib\site-packages\xlrd\__init__.py", line 441, in open_workbook
    ragged_rows=ragged_rows,
  File "C:\ProgramData\Anaconda3\lib\site-packages\xlrd\book.py", line 87, in open_workbook_xls
    ragged_rows=ragged_rows,
  File "C:\ProgramData\Anaconda3\lib\site-packages\xlrd\book.py", line 595, in biff2_8_load
    raise XLRDError("Can't find workbook in OLE2 compound document")
xlrd.biffh.XLRDError: Can't find workbook in OLE2 compound document

我的 Excel 文件是 xlsm 并受密码保护。 OLE2 复合文件到底是什么意思? pandas 打开这种 Excel 文件有问题吗?我正在使用 python v3.6

【问题讨论】:

  • 我使用的是 xlrd 1.0.0。这是最新版本。我刚刚做了一个conda update
  • 我之前在受保护的文件方面也遇到过类似的问题。文件上是否有任何保护/锁定?
  • @ayhan,Excel 文件受密码保护。
  • 我使用this 删除了对文件的保护并读取了文件:我找不到其他解决方案。它只适用于 Windows。
  • 谢谢。我想你已经得到了答案。为了使 xlrd 工作,必须从 Excel 中删除保护。我刚刚发现了另一种打开受保护的 Excel 文件的方法。 xlwings 能够读取受保护的 Excel 文件。我认为 xlwings 是适合我的方式。

标签: python excel python-3.x pandas


【解决方案1】:

我会回答我自己的问题。在 ayhan 的其中一个 cmets 中,xlrd 无法读取受 Excel 保护的文件。一种解决方案是移除保护。

I need the command to unprotect an Excel file from python

读取受 Excel 保护的文件的另一种解决方案是使用 xlwings。我已验证 xlwings 在打开 Excel 文件时能够读取受保护的 Excel 文件。

【讨论】:

    猜你喜欢
    • 2020-12-04
    • 1970-01-01
    • 2015-01-23
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    • 1970-01-01
    • 2018-04-01
    相关资源
    最近更新 更多