如何在excel工作表之间进行更改?

2024-05-29 05:53:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有更多的excel文件在一个目录中,我想从中提取数据(从不同的单元格)。如何应用循环从第一个文件的第一个工作表获取数据,然后移动到第二个工作表并从第二个工作表获取数据

for file in glob.glob('N:\mydata\*.xls'):
    book = xlrd.open_workbook(file, formatting_info=True, encoding_override="utf-8")
    sheets = book.sheet_names()

    for index, sh in enumerate(sheets):
        sheet = book.sheet_by_index(index)
        rows, cols = sheet.nrows, sheet.ncols
        print "Number of rows: %s   Number of cols: %s" % (rows, cols)

        for row in range(rows):
            for col in range(cols):
                thecell = sheet.cell(row, col)
                ertek = thecell.value
                xfx = sheet.cell_xf_index(row, col)
                xf = book.xf_list[xfx]
                bgx = xf.background.pattern_colour_index
                pattern_colour = book.colour_map[bgx]

                # mydata
                if row == 7 and col == 9:
                    mydata= ertek
                    print mydata

Tags: 文件inforindexcolglobfilesheet

热门问题