读取 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
我在给定的文件夹中有一堆 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