python pandas打开excel语句:pd.ExcelFile(r'D:\demo.xlsx')怎样用一个变量代替文件地址字符串?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python pandas打开excel语句:pd.ExcelFile(r'D:\demo.xlsx')怎样用一个变量代替文件地址字符串?相关的知识,希望对你有一定的参考价值。

python pandas打开excel语句,如:demo_excel = pd.ExcelFile(r'D:\demo.xlsx')怎样将'D:\demo.xlsx'用一个变量替代,用变量的值读取相应的excel文件?

参考技术A FILE_PATH = ‘D:\demo.xlsx’.追问

如果FILE_PATH = ‘D:\demo.xlsx’
那下面这条语句如何写?
demo_excel = pd.ExcelFile( r FILE_PATH)
括号里的FILE_PATH前面还有一个 r 。

追答

可以写demo_excel=pd.ExcelFile(repr(FILE_PATH)).

Pandas 无法打开此 Excel 文件

【中文标题】Pandas 无法打开此 Excel 文件【英文标题】:Pandas unable to open this Excel file 【发布时间】: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 是适合我的方式。 【参考方案1】:

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

I need the command to unprotect an Excel file from python

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

【讨论】:

以上是关于python pandas打开excel语句:pd.ExcelFile(r'D:\demo.xlsx')怎样用一个变量代替文件地址字符串?的主要内容,如果未能解决你的问题,请参考以下文章

python pandas 打开excel很慢?

Python:Pandas read_excel 无法打开 .xls 文件,不支持 xlrd

pandas常用语句

pandas常用语句

python之pandas核心函数

python pandas读写excel