Pandas 无法打开这个 Excel 文件

use*_*486 9 python excel python-3.x pandas

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

import pandas as pd
df = pd.read_excel('../TestXLWings.xlsm', sheetname="TestSheet")
Run Code Online (Sandbox Code Playgroud)

我在下面遇到错误;

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
Run Code Online (Sandbox Code Playgroud)

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

use*_*486 16

我会回答我自己的问题。在 ayhan 的评论之一中,xlrd 无法读取受 Excel 保护的文件。一种解决方案是取消保护。

我需要从 python 中取消保护 Excel 文件的命令

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