我正在尝试制作一个可以从Excel工作表中读取值并计算给定运算符的答案的计算器。我想不出一种写它的方式,这样它就可以访问存储在元组中的值,而该元组正在以[
的形式读取单元格值代码:
def excel_math(filename):
from openpyxl import load_workbook
wb = load_workbook(filename)
ws = wb.active
for row in ws.rows:
operator = row[0].value
nums = row[1:]
if operator == '+':
subtotal = int(nums[0])
for numcell in nums[1:]:
subtotal += int(nums)
print(subtotal)
def excel_math(filename):
from openpyxl import load_workbook
wb = load_workbook(filename)
ws = wb.active
for row in ws.rows:
operator = row[0]
nums = row[1:]
if operator == '+':
subtotal = int(nums[0])
for numcell in nums:
subtotal += int(numcell)
print(subtotal)
我认为这应该起作用
openpyxl和Python为此类任务提供了几个方便的功能。首先,您只应将相关代码放入函数中,因此您可能需要一个单独的函数来传递工作表。
def row_summer(ws):
for row in ws.iter_rows(values_only=True):
op, *values = row
if op == "+":
return sum(values)