Python Excel - 如何将工作表名称转换为工作表编号 [英] Python Excel - How to turn sheet name into sheet number

查看:763
本文介绍了Python Excel - 如何将工作表名称转换为工作表编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个程序中,我在输入excel文件中创建一个名为new_sheet的工作表。



我需要工作表的工作表编号,而不必查看excel文件。



如何从计划返回表格编号?

  import xlwt 
import xlrd
import csv

workbook = xlrd.open_workbook('input.xls')

worksheet = workbook.add_sheet('new_sheet')


解决方案

如果您想计算Excel文件中有多少页,如果你想知道,给出的号码对应的表的名称;
无论如何,如果你想计算一个Excel文件中有多少张纸,你可以这样继续:

  workbook = xlrd.open_workbook('input.xls')
worksheet = workbook.add_sheet('new_sheet')

#页数
打印workbook.nsheets
相反,如果您想从名称中获知相应的工作表数量,您可以按照以下方式进行:

  workbook = xlrd.open_workbook('input.xls',on_demand = True)
索引,枚举中的工作表(workbook.sheet_names ()):
if sheet ==<您的工作表名称>:
打印索引

使用 on_demand = True ,您可以打开文件不自动加载。



尊敬


In this program I create a sheet on the input excel file called new_sheet.

I need the sheet number of the sheet without having to look at the excel file.

How do I return the sheet number from the program?

import xlwt
import xlrd
import csv

workbook = xlrd.open_workbook('input.xls')

worksheet = workbook.add_sheet('new_sheet')

解决方案

I'm not understand well if you want count how many sheets there are in your Excel file or if you want know, gave the name of sheet at which number correspond; anyway, if you want count how many sheets there are in an Excel file you can proceed in this way:

workbook = xlrd.open_workbook('input.xls')
worksheet = workbook.add_sheet('new_sheet')

# number of sheet
print workbook.nsheets

Instead, if you want know the corresponding number of sheets from the name you can proceed in this way:

workbook = xlrd.open_workbook('input.xls', on_demand=True)
for index, sheet in enumerate(workbook.sheet_names()):
    if sheet == <name of your sheet>:
        print index

With on_demand=True, you can open file not loading automatically.

Regards

这篇关于Python Excel - 如何将工作表名称转换为工作表编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆