简体   繁体   English

如何使用 openpyxl 在一系列列中动态编写 excel 公式(SUM())?

[英]How to write excel formula (SUM()) dynamically in a range of columns using openpyxl?

In my Excel spreadsheet I need to enter excel formula on a bottom that will summarize values.在我的 Excel 电子表格中,我需要在底部输入 excel 公式来汇总值。 Number of rows can be different.行数可以不同。 But not columns.但不是专栏。

So in cell B10 should be =SUM(B2:B7)所以在单元格 B10 中应该是=SUM(B2:B7)

in cell C10 should be =SUM(C2:C7)在单元格 C10 中应该是=SUM(C2:C7)

in cell D10 should be =SUM(D2:D7)在单元格 D10 中应该是=SUM(D2:D7)

and so on...等等...

在此处输入图像描述

import openpyxl

wb = openpyxl.load_workbook("C:\\Users\\my\\Test.xlsx")
   
# open sheet to write into. 
ws =  wb.active

#Get the number of rows to make it easier to add our Excel formulas a little later
last_row = ws.max_row   


for col in ws.iter_cols(min_row=last_row+2, min_col=14, max_row=last_row+2, max_col=28):
    for cell in col:
        cell.value = '=SUM(A2:B2)' # hou to make formula dynamic?

You can do this where你可以在哪里做

  1. first_row is the start of the summing rows first_row 是求和行的开始
  2. sum_row is the row where the totals will be placed. sum_row 是将放置总计的行。 Here its 3 rows down from last_row这是从 last_row 向下的 3 行
  3. start_col is the first column to add the SUM formula ie col B start_col 是添加 SUM 公式的第一列,即 col B
  4. end_col is the last column to add the SUM formula ie col M end_col 是添加 SUM 公式的最后一列,即 col M

... ...

first_row = 2
last_row = ws.max_row
sum_row = last_row + 3
start_col = 2
end_col = 13
for row in ws.iter_rows(min_row=sum_row, max_row=sum_row, min_col=start_col, max_col=end_col):
    for cell in row:
        cell_sum_start = cell.column_letter + str(first_row)
        cell_sum_end = cell.column_letter + str(last_row)
        cell.value = '=SUM({0}:{1})'.format(cell_sum_start, cell_sum_end)

The code looks good.代码看起来不错。 I assume that the only problem is the formula itself: how to make '=SUM(A2:A10)' and then '=SUM(B2:B10)' automatically.我认为唯一的问题是公式本身:如何自动生成'=SUM(A2:A10)'然后'=SUM(B2:B10)' What you are looking for are the functions ord() and chr() :您正在寻找的是功能ord()chr()

last_row = 42
for i in range(26):
   current_col_num = ord('A') + i
   current_col = chr(current_col_num)
   print(f'=SUM({current_col}2:{current_col}{last_row})')

You may want to wrap after 26 characters to get this 'Z', 'AA' behavior in the names if you have large numbers.如果数字很大,您可能希望在 26 个字符后换行以获得名称中的“Z”、“AA”行为。 See this answer how you can iterate over a list of wrapping characters.请参阅此答案,了解如何迭代换行符列表。

暂无
暂无

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

相关问题 如何使用 xlsxwriter 模块在多个单元格中的 excel 中动态编写公式? - How to write formula dynamically in excel in multiple cells using xlsxwriter module? 如何使用 openpyxl 在特定列上写入总计 - How to write totals on a particular columns using openpyxl 如何使用openpyxl使用列表写入Excel单元格区域? - How can I use a list to write into an Excel cell range using openpyxl? 如何使用openpyxl和python3为excel工作表中的一系列单元格(列和行)提供字体颜色? - How to give font color to a range of cells (columns and rows) in excel worksheet using openpyxl and python3? 如何使用openpyxl将数据透视表值(列)从熊猫写入Excel? - How to write pivot table values (columns) from pandas to excel using openpyxl? 无法使用 openpyxl 将单元格引用到 excel 公式中 - Unable to refernce a cell into an excel formula using openpyxl OpenPyxl,使用 .format() 跨列循环公式? - OpenPyxl, loop a formula across columns using .format()? 使用 openpyxl python 在 excel 中插入带空白的公式 - Inserting formula with Blank in excel using openpyxl python 如何使用 Python(openpyxl 或其他)将 excel 公式应用于多个工作簿中特定单元格范围内的一个工作表 - How to apply an excel formula into one worksheet in a specific range of cells over multiple workbooks with Python (openpyxl or other) 如何使用 openpyxl 在 Excel 工作表中插入数组公式? - How to insert array formula in an Excel sheet with openpyxl?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM