使用 Python 仅打印工作表名称
Print just the sheetnames with Python
如何只打印 sheet 个名字。我使用一个循环获取范围 2 到 9 的 sheets 并打印,但我想删除它周围的标签,只打印 sheet.
的名称
import glob
import openpyxl
path = 'C:/ExcelFolder/*.xlsx'
files = glob.glob(path)
for file in files:
wb = openpyxl.load_workbook(file)
for n in range(2, 9):
SheetName = wb.worksheets[n]
print(SheetName)
输出示例:
<Worksheet "Monday">
<Worksheet "Tuesday">
<Worksheet "Wednesday">
<Worksheet "Thursday">
<Worksheet "Friday">
<Worksheet "Saturday">
怎么样:print(wb.sheetnames)
from openpyxl.reader.excel import load_workbook
import glob
path = '*.xlsx'
files = glob.iglob(path)
for fileName in files:
wb=load_workbook(fileName)
w = wb.sheetnames
for sheetname in w:
print sheetname
两种方法。第一个类似于@Gopal Choudhary
的回答
对于 .xlsx
,您可以使用 glob 和 openpyxl。
path = 'C:path/*.xlsx'
files = glob.iglob(path)
for fileName in files:
wb = openpyxl.load_workbook(fileName)
w = wb.sheetnames
for sheetname in w:
print(sheetname)
对于 .xls
你可以使用 pandas.
sheet_to_df_map = pd.read_excel('file_name', sheet_name=None)
如果您正在使用 pandas read_excel 方法。
您可以使用字典键方法检索 sheet 名称,因为 sheet 名称存储在字典中。
xls = pd.read_excel(url, sheet_name = None)
print(xls.keys())
如何只打印 sheet 个名字。我使用一个循环获取范围 2 到 9 的 sheets 并打印,但我想删除它周围的标签,只打印 sheet.
的名称import glob
import openpyxl
path = 'C:/ExcelFolder/*.xlsx'
files = glob.glob(path)
for file in files:
wb = openpyxl.load_workbook(file)
for n in range(2, 9):
SheetName = wb.worksheets[n]
print(SheetName)
输出示例:
<Worksheet "Monday">
<Worksheet "Tuesday">
<Worksheet "Wednesday">
<Worksheet "Thursday">
<Worksheet "Friday">
<Worksheet "Saturday">
怎么样:print(wb.sheetnames)
from openpyxl.reader.excel import load_workbook
import glob
path = '*.xlsx'
files = glob.iglob(path)
for fileName in files:
wb=load_workbook(fileName)
w = wb.sheetnames
for sheetname in w:
print sheetname
两种方法。第一个类似于@Gopal Choudhary
的回答对于 .xlsx
,您可以使用 glob 和 openpyxl。
path = 'C:path/*.xlsx'
files = glob.iglob(path)
for fileName in files:
wb = openpyxl.load_workbook(fileName)
w = wb.sheetnames
for sheetname in w:
print(sheetname)
对于 .xls
你可以使用 pandas.
sheet_to_df_map = pd.read_excel('file_name', sheet_name=None)
如果您正在使用 pandas read_excel 方法。
您可以使用字典键方法检索 sheet 名称,因为 sheet 名称存储在字典中。
xls = pd.read_excel(url, sheet_name = None)
print(xls.keys())