Pandas无法打开此Excel文件

bwitn5fc  于 2023-01-14  发布在  其他
关注(0)|答案(2)|浏览(367)

我正在尝试用pythonPandas打开一个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,有密码保护,OLE 2复合文档到底是什么意思,Pandas打开这种Excel文件有问题吗,我用的是python v3.6

vsikbqxv

vsikbqxv1#

我会回答我自己的问题。在ayhan的一个评论中,Excel保护的文件不能被xlrd读取。一个解决办法是取消保护。
我需要这个命令来解除python对Excel文件的保护
读取Excel保护文件的另一个解决方案是使用xlwings。我已经验证了xlwings能够在打开Excel文件时读取受保护的Excel文件。

q8l4jmvw

q8l4jmvw2#

我会创建一个新的excel文件,并删除excel中的敏感性标签。然后就可以用pd读取该文件了。

相关问题