pandas 如何使用 Python 计算 Excel 文件中的总页数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50950359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:43:08  来源:igfitidea点击:

How to count the total number of sheets in an Excel file using Python

pythonexcelpandasopenpyxlxlrd

提问by Shahed Adnan

I am reading a excel file using python.

我正在使用 python 读取 excel 文件。

import pandas as pd
import os

xls = pd.ExcelFile('D:\DirectoryProject\Mapping.xlsx')

It has several number of data sheets which I don't know. How can I count the total number of sheets in Mapping.xlsxfile using Python?

它有几个我不知道的数据表。如何Mapping.xlsx使用 Python 计算文件中的总页数?

回答by jpp

openpyxl

openpyxl

import openpyxl

wb = openpyxl.load_workbook('file.xlsx') 
res = len(wb.sheetnames)

pandas

pandas

import pandas as pd

xl = pd.ExcelFile('file.xlsx')
res = len(xl.sheet_names)

xlrd

xlrd

import xlrd

# use on_demand=True to avoid loading worksheet data into memory
wb = xlrd.open_workbook('file.xlsx', on_demand=True)
res = len(wb.sheet_names())  # or wb.nsheets

回答by Sankar

Just to add to the previous answer -

只是为了添加到以前的答案 -

len(pd.read_excel(r"D:\DirectoryProject\Mapping.xlsx", sheet_name=None))

This way you can get the number of sheets as well.

这样你也可以得到张数。