繁体   English   中英

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

[英]Reading a cell value in Excel and write into an othet existing Excel file with python

我在给定的文件夹中有一堆 xls 文件。 我想一一打开它们获取给定的单元格值并写入另一个现有(半满)excel文件(到给定位置)。 这是我的代码。 它返回该行的错误,该行定义了我要写入数据的位置。 它说: AttributeError: 'Worksheet' object has no attribute 'range' 你能帮帮我吗?

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")

dir(Sheet1) 不显示范围作为选项。 要编辑单元格 (2,4) 的值,请使用

Sheet1.cell(row=2,column=4).value = utkategoria 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM