编程语言
首页 > 编程语言> > python – 调用while循环的工作表名称

python – 调用while循环的工作表名称

作者:互联网

我已导入xlrd等.我的代码的主要部分如下:

for serie_diam in range(0,9):
namesheet = "Diamètre " + str(serie_diam)
#select(namesheet)
numLine = sh.row_values(3)
OK = 1
while OK == 1:
    d = sh1(numLine, 1)
    D = sh1(numLine, 2)
    rs = sh1(numLine, 7)
    for i in range(4):
        BB = sh1(numLine, 2 + i)
        if BB != 0:
            print repr(d).rjust(2), repr(D).rjust(3), repr(B).rjust(4), repr(rs).rjust(5)

我总共在我的xls文件中有7张纸,我想知道如何在同一个循环中循环这些,如OK == 1那时我只写了’sh1′.

如果这个问题太容易了,我很抱歉!

解决方法:

import xlrd

book = xlrd.open_workbook('xlrd_test.xls')

for sheet in book.sheets():
    print sheet.row(0) # do stuff here - I'm printing the first line as example

# or if you need the sheet index for some purpose:
for shidx in xrange(0, book.nsheets):
    sheet = book.sheet_by_index(shidx)
    # would print 'Page N, first line: ....'
    print 'Page %d, first line: %s' % (shidx, sheet.row(0))

标签:python,export-to-excel,worksheet,xlrd
来源: https://codeday.me/bug/20190613/1235137.html