在Excel中读取单元格值并使用python写入现有Excel文件

2024-09-27 00:14:06 发布

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

我在给定的文件夹中有一堆xls文件。我想一个接一个地打开它们,得到一个给定的单元格值,然后写入另一个现有的(半填充的)excel文件(在给定的位置)。 这是我的密码。它为行返回一个错误,该错误定义了我要写入数据的位置。它说:AttributeError:“工作表”对象没有属性“范围” 你能帮帮我吗

for file in glob.glob('N:\*.xls'):
    fajlneve = str(file)
    fajlneve = fajlneve.decode('latin-1')
    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
        for row in range(rows):
            for col in range(cols):
                # print "row, col is:", row+1, col+1,
                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]
                if row == 19 and col == 11 and index == 1:
                    utkategoria = ertek
                    print utkategoria
                    wb = load_workbook(
                        "N:\map\new.xlsx")
                    sheets = wb.sheetnames
                    Sheet1 = wb[sheets[0]]
                    print Sheet1
                    Sheet1.range(2, 4).value = utkategoria  # This will change the cell(2,4) to 4
                    wb.save("N:\map\new_v2.xlsx")

Tags: inforindexcellrangecolfilesheet

热门问题