简体   繁体   English

Pandas 无法打开这个 Excel 文件

[英]Pandas unable to open this Excel file

I am trying to use python pandas to open an Excel file.我正在尝试使用 python pandas 打开一个 Excel 文件。 Code is simple as shown below;代码很简单,如下图;

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.我的 Excel 文件是 xlsm 并受密码保护。 What does OLE2 compound document mean exactly? OLE2 复合文档到底是什么意思? Does pandas have problems opening this kind of Excel files? pandas打开这种Excel的文件有问题吗? I am using python v3.6我正在使用 python v3.6

I will answer my own question. 我会回答我自己的问题。 In one of the comments from ayhan, Excel-protected files cannot be read by xlrd. 在ayhan的一条评论中,xlrd无法读取受Excel保护的文件。 One solution is to remove the protection. 一种解决方案是取消保护。

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. 读取受Excel保护的文件的另一个解决方案是使用xlwings。 I have verified that xlwings is able to read protected Excel files when the Excel file is opened. 我已经验证xlwings能够在打开Excel文件时读取受保护的Excel文件。

I would create a new excel file and remove sensitivity label in excel. Then be able to read the file with pd.我会创建一个新的 excel 文件并删除 excel 中的敏感度 label。然后能够使用 pd 读取该文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM