我们知道如何使用 Polar 读取 Excel 文件。
Import polars as pl
df = pl.read_excel()
在 pandas 中,我们有能力获取所有工作表名称。我们在 Polars 中有这样的功能吗?
xl = pd.ExcelFile('file.xlsx')
# Get sheet names
sheet_names = xl.sheet_names
您可以在
PlExcelFile
的帮助下制作自己的
_initialise_spreadsheet_parser
:
from polars.io.spreadsheet.functions import _initialise_spreadsheet_parser
class PlExcelFile:
def __init__(self, source, engine="xlsx2csv", engine_options={}):
self.source = source
self.engine = engine
self.engine_options = engine_options
@property
def sheet_names(self):
detail_sheets = _initialise_spreadsheet_parser(
engine=self.engine,
source=self.file_path,
engine_options=self.engine_options
)[-1]
return [d["name"] for d in detail_sheets]
pl_xl = PlExcelFile("file.xlsx")
# pl_xl.sheet_names # Sheet1, Sheet2, ...