问题描述
我有一个包含多列的Excel电子表格。 如何遍历特定的列并在底部创建一个总和。行数是动态的。
解决方法
一个简单但不错的解决方案:
def sum_col(ws,col,col_top=2,tight=False):
col_len = len(ws[col])
if tight:
col_len -= next(i for i,x in enumerate(reversed(ws[col])) if x.value is not None)
ws[f'{col}{col_len + 1}'] = f'=SUM({col}{col_top}:{col}{col_len})'
如果您的示例数据sums.xlsx
如下所示:
"a",1,"b",2,"c"
"d","e","f"
"g",3,"h"
"i",
然后这样:
from openpyxl import load_workbook
def sum_col(ws,x in enumerate(reversed(ws[col])) if x.value is not None)
ws[f'{col}{col_len + 1}'] = f'=SUM({col}{col_top}:{col}{col_len})'
wb = load_workbook('sums.xlsx')
ws = wb.active
sum_col(ws,'B',col_top=1)
sum_col(ws,'D',col_top=1,tight=True)
wb.save('changed_sums.xlsx')
会导致:
"a",4,6,
(其中6
和4
当然是SUM的结果)
请注意,传递col_top=1
是因为该函数假定只有一个行标题(示例中没有)。并且tight
参数使脚本找到列的实际结尾(第一个非无单元格),而不是openpyxl
认为是列的结尾(基本上只是行数)数据)。
这可以解决问题。但是,如果有人知道更好的方法,请提供。
from openpyxl import load_workbook
from xlsxwriter.utility import xl_rowcol_to_cell
# Get the number of rows
df_len = len(df.index)
# list of column numbers that need to show sum at the bottom
col = [0,2]
# iterating through those columns
for column in col:
# Determine where we will place the formula
cell_location = xl_rowcol_to_cell(df_len + 1,column) # xl_rowcol_to_cell(RowNumber,ColumnNumber)
# Get the range to use for the sum formula
start_range = xl_rowcol_to_cell(1,column) # row 1 (start from 0!) and column 6 and so on
end_range = xl_rowcol_to_cell(df_len,column)
# Construct and write the formula
formula = "=SUM({:s}:{:s})".format(start_range,end_range) # {:s} substituted with "start_range" and "end_range" accordingly
cell = ws[cell_location]
cell.value = formula
wb.save('changed_sums.xlsx')