使用 xlwt 理解 for 循环
Understanding for loop with xlwt
enter image description here我想用 XLWT 进行计算我是编码新手,对使用 for 循环重复以下各列中的公式有点困惑,请帮助我理解 for 循环函数下面计算
请参考图片,我需要一个 for 循环来填充其余的列,这样我就不必输入 'w_sheet.write(1,2,xlwt.Formula('A2-B2'))' 多次
import xlrd
import xlwt
from xlutils.copy import copy
wb = xlrd.open_workbook('Subtract.xls')
rb = copy(rb)
w_sheet = wb.get_sheet(0)
w_sheet.write(1,2,xlwt.Formula('A2-B2'))
w_sheet.write(2,2,xlwt.Formula('A3-B3'))
w_sheet.write(3,2,xlwt.Formula('A4-B4'))
w_sheet.write(4,2,xlwt.Formula('A4-B4'))
wb.save('Subtract.xls')
嗨,试着理解:D
import xlrd
import xlwt
from xlutils.copy import copy
import os
file_path = "test.xls"
rb = xlrd.open_workbook(file_path, formatting_info=True)
wb = copy(rb)
w_sheet = wb.get_sheet(0)
# For each sheet, do operation
for sheet in rb.sheets():
# For each row in actual sheet, row += 1 at each operation
for row in range(sheet.nrows - 1):
# Set index for operation and result
index = row + 2
# Create operation
operation = 'A' + str(index) + '-B' + str(index)
# Use operation
w_sheet.write(index-1, 2, xlwt.Formula(operation))
# Print for you can understand what it do
print ("index = " + str(index) + " operation = " + operation)
# Save in file_path.out.xls
wb.save(file_path + '.out' + os.path.splitext(file_path)[-1])
enter image description here我想用 XLWT 进行计算我是编码新手,对使用 for 循环重复以下各列中的公式有点困惑,请帮助我理解 for 循环函数下面计算
请参考图片,我需要一个 for 循环来填充其余的列,这样我就不必输入 'w_sheet.write(1,2,xlwt.Formula('A2-B2'))' 多次
import xlrd
import xlwt
from xlutils.copy import copy
wb = xlrd.open_workbook('Subtract.xls')
rb = copy(rb)
w_sheet = wb.get_sheet(0)
w_sheet.write(1,2,xlwt.Formula('A2-B2'))
w_sheet.write(2,2,xlwt.Formula('A3-B3'))
w_sheet.write(3,2,xlwt.Formula('A4-B4'))
w_sheet.write(4,2,xlwt.Formula('A4-B4'))
wb.save('Subtract.xls')
嗨,试着理解:D
import xlrd
import xlwt
from xlutils.copy import copy
import os
file_path = "test.xls"
rb = xlrd.open_workbook(file_path, formatting_info=True)
wb = copy(rb)
w_sheet = wb.get_sheet(0)
# For each sheet, do operation
for sheet in rb.sheets():
# For each row in actual sheet, row += 1 at each operation
for row in range(sheet.nrows - 1):
# Set index for operation and result
index = row + 2
# Create operation
operation = 'A' + str(index) + '-B' + str(index)
# Use operation
w_sheet.write(index-1, 2, xlwt.Formula(operation))
# Print for you can understand what it do
print ("index = " + str(index) + " operation = " + operation)
# Save in file_path.out.xls
wb.save(file_path + '.out' + os.path.splitext(file_path)[-1])