简体   繁体   English

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

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

I have a bunch of xls files in a given folder.我在给定的文件夹中有一堆 xls 文件。 I want to open them one by one get a given cell value and write into an other existing (half filled) excel file (in to a given place).我想一一打开它们获取给定的单元格值并写入另一个现有(半满)excel文件(到给定位置)。 Here is my code.这是我的代码。 It gives back an error for the row, which defines the place where I want to write the data.它返回该行的错误,该行定义了我要写入数据的位置。 It says: AttributeError: 'Worksheet' object has no attribute 'range' Can you please help me?它说: 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) does not show range as an option. dir(Sheet1) 不显示范围作为选项。 To edit the value of the cell (2,4), please use要编辑单元格 (2,4) 的值,请使用

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

暂无
暂无

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

相关问题 如何在 Python 中现有的 excel 文件中向特定单元格插入值? - How to insert a value to a specific cell in an existing excel file in Python? Python Excel:如何向现有 excel 文件中的每个单元格添加一个数字,然后将新值发送回 excel 文件,然后将新值发送回 excel 文件,然后将新值发送回 excel 文件,然后将新值发送回 excel 文件 - Python Excel: how to add a number to each cell in an existing excel file and then send the new value back to excel document 读取 excel 文件,将每个单元格值提取为 str 并对每个单元格值执行一些操作,Python - Reading an excel file, extracting each cell value as a str and doing some action on each cell value, Python Python-Excel:如何将一行行写入到现有的excel文件中? - Python-Excel: How to write lines of a row to an existing excel file? Python pandas:将变量写入现有工作表中的 excel 单元格 - Python pandas: Write variable to excel cell in existing sheet 在python中读取Excel文件 - Reading an Excel file in python Python - 在所有现有值之后向 Excel 的单元格添加一个值 - Python - Add a value to an Excel's cell after all the existing values python 写入 excel 覆盖单元 - python write to excel overwrite cell 使用python将文本添加到现有excel文件的特定列的每个单元格中 - Add text to each cell of a particular column of an existing excel file with python 如何从 Python 中的现有文件在 Excel 中编写列标题 - How to write a column header in Excel from an existing file in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM