简体   繁体   English

使用熊猫,如何只从 Excel 工作簿中读取一张工作表而不加载任何其他工作表?

[英]Using pandas, how to only read one sheet from Excel workbook without loading any of the other sheets?

This is a little different from similar questions to this.这与类似的问题有点不同。 I am using the FactSet plugin for Excel, if anyone's familiar.如果有人熟悉,我正在使用 Excel 的 FactSet 插件。 I have a workbook with multiple tabs that use the FactSet plugin to pull in data.我有一个带有多个选项卡的工作簿,这些选项卡使用 FactSet 插件来提取数据。 No issues with this.这没有问题。

The problem is FactSet generates a hidden cache sheet for every Excel workbook that when read in causes loading any sheet of the Excel file by python to return an empty DataFrame.问题是 FactSet 为每个 Excel 工作簿生成一个隐藏的缓存工作表,当读入时会导致 python 加载 Excel 文件的任何工作表以返回一个空的 DataFrame。

I've tried two methods to get the data from the sheet, but both methods load the entire workbook causing the FactSet cache file to be read in.我尝试了两种方法来从工作表中获取数据,但这两种方法都会加载整个工作簿,从而导致读入 FactSet 缓存文件。

import pandas as pd

fname = 'facset_excel_file.xlsm'

# method 1
wb = pd.ExcelFile(fname)
symbols = pd.read_excel(wb, sheet_name='Symbols')  # returns empty DataFrame

# method 2
pd.read_excel(fname, sheet_name='Symbols')  # returns empty DataFrame

So my questions are how can I read in a single sheet without loading the entire workbook?所以我的问题是如何在不加载整个工作簿的情况下阅读单个工作表? Or is it possible to exclude a worksheet by name when loading a workbook?或者是否可以在加载工作簿时按名称排除工作表?

You can use something like, the file can be read using the file name as string or an open file object:您可以使用类似的东西,可以使用文件名作为字符串或打开的文件对象读取文件:

df = pd.read_excel(open('facset_excel_file.xlsm', 'rb'), sheet_name='123')

Try it.尝试一下。

暂无
暂无

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

相关问题 从 pandas 到一张 excel 工作表的多个数据帧,而不用 python 删除其他工作表 - multiple dataframes from pandas to one excel sheet without deleting other sheets with python 如何使用 Pandas 从 Excel 中只读取可见的工作表? - How to read only visible sheets from Excel using Pandas? 如何使用python将数据从一个工作表添加到另一个工作簿中的现有工作表而不覆盖其他工作表的数据? - How to add data using python from one sheet to an existing sheet in another workbook without overwriting other sheet's data? 我可以从Excel文件修改特定工作表并回写到同一工作表而不使用Pandas |修改其他工作表 openpyxl - Can I modify specific sheet from Excel file and write back to the same without modifying other sheets using Pandas | openpyxl 熊猫read_excel,第一张纸中有1行标题,但其他纸中没有 - Pandas read_excel, 1 row header in first sheet but not other sheets 从 Excel 工作簿的每张纸中读取一个单元格并编译成一个列表(熊猫/python) - Read one cell from each sheet of an Excel workbook and compile into a list (pandas/python) 如何使用 python 将 excel 工作簿中的内容复制到另一个工作簿而不丢失 excel 格式 - How to copy contents from a sheet of an excel workbook to another workbook without loosing the excel formatting using python 用 pandas dataframe 覆盖 excel 工作表而不影响其他工作表 - Overwrite an excel sheet with pandas dataframe without affecting other sheets 如何使用 Python 将数据从一个 Excel 工作表复制到同一工作簿的另一工作表? - How to copy data from One Excel sheet to another sheet for same Workbook Using Python? 使用Python(和DataNitro)将单元格从一个Excel工作簿中的特定工作表复制到另一个Excel工作簿中的特定工作表 - Using Python (and DataNitro) to copy cells from a particular sheet in one Excel workbook, to a particular sheet in another Excel workbook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM