【问题标题】:Openpyxl & Python : column of keys, column of values - how to add up the values and assign totals to corresponding keysOpenpyxl 和 Python:键列、值列 - 如何将值相加并将总计分配给相应的键
【发布时间】:2017-07-17 21:11:48
【问题描述】:

为错误的开始道歉。我现在已经阅读了常见问题解答,并希望我的问题符合标准:)。我在电子表格中有以下内容:

Col1        Col2
1234        12.5
1234        8.2
1234        9.8
2334        10.1
2334        7.7
4567        9.8
5678        9.9
5678        8.4

我需要使用 OpenPyxl 和 Python ie 将 Col2 中的每个参考号的数字加起来。

1234        30.5
2334        17.8
4567        9.8
5678        18.3

在几次错误的开始之后,我有了这个:

#import modules
import openpyxl
from openpyxl.utils import coordinate_from_string, column_index_from_string
from openpyxl.utils.cell import _get_column_letter
import sys
from datetime import date
from datetime import time
import datetime
import calendar
from openpyxl.styles import Color, PatternFill, Font, Border
from shutil import copyfile

#set variables
dest_filename = 'P:\\Charging\\Chargeable Resources\\ChargeableActivity\\January2017ChargeableActivity.xlsx'
total = 0

#create objects
wb = openpyxl.load_workbook(filename = dest_filename, data_only=True)
ws1 = wb.get_sheet_by_name('ChargeableActivity')

for i in range(1, ws1.max_row):
    #convert ws1.cell(row=i, column=12).value to integer (=Excel convert to number)
    if isinstance(ws1.cell(row=i, column=12).value,long):
        RFCNumber = ws1.cell(row=i, column=12).value
        for col in ws1.iter_cols(min_col=12, max_col = 12, min_row=1):
            if ws1.cell(row=i, column=12).value == RFCNumber:
                total = total + ws1.cell(row=i, column=14).value
                print(RFCNumber,'Total=',total)

但输出是累积的,不会删除重复的 RFC 编号:

Col1        Col2
    1234        12.5
    1234        20.7
    1234        30.5
    2334        40.6        
    4567        48.3        
    5678        58.1

等 我不是编码员,我正在寻找一种方法来节省大量编辑大型电子表格的时间。欢迎任何建议。谢谢。

【问题讨论】:

  • 如果你已经在这几天了,你一定写过一些代码。你应该向我们展示你到目前为止所拥有的,并解释它有什么问题。
  • 感谢@DanielRoseman - 第一次发帖。无法在我的回复中插入代码,因为编辑器没有弹出并且代码被破坏。我可以重新开始还是被逐出社区?
  • 插入代码作为对问题的编辑。

标签: python excel openpyxl


【解决方案1】:

试试这个代码:

# Define a dict{} for key:value pairs
ref_total = {}

# Get Data from all rows
for row in ws.rows:
    # Slice cells A,B from row tuple
    cell_A = row[:1][0]
    cell_B = row[1:2][0]

    reference = cell_A.value
    if reference in ref_total.keys():
        ref_total[reference] += cell_B.value
    else:
        ref_total[reference] = cell_B.value

for key in sorted(ref_total.keys()):
    print('%s %s' % (key, ref_total[key]))

使用 Python:3.4.2 - openpyxl:2.4.1 - LibreOffice:4.3.3.2 测试

【讨论】:

  • 很棒的东西@stovfl - 它有效。我不得不在它周围加上一些逻辑来阻止 None 值被累加,但仅此而已。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-31
  • 2020-12-21
  • 1970-01-01
  • 2014-07-06
相关资源
最近更新 更多