Pandas 无法打开这个 Excel 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45725645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:15:59  来源:igfitidea点击:

Pandas unable to open this Excel file

pythonexcelpython-3.xpandas

提问by user781486

I am trying to use python pandas to open an Excel file. Code is simple as shown below;

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

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

I got an error below;

我在下面遇到错误;

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

My Excel file is xlsm and protected by password. What does OLE2 compound document mean exactly? Does pandas have problems opening this kind of Excel files? I am using python v3.6

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

回答by user781486

I will answer my own question. In one of the comments from ayhan, Excel-protected files cannot be read by xlrd. One solution is to remove the protection.

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

I need the command to unprotect an Excel file from python

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

Another solution to read the Excel-protected file is to use xlwings. I have verified that xlwings is able to read protected Excel files when the Excel file is opened.

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