Przepraszamy za fałszywy start. Przeczytałem często zadawane pytania i mam nadzieję, że moje pytanie spełni standardy :). Mam w arkuszu kalkulacyjnym:
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
muszę sumować liczby w kolumnie 2 dla każdego numeru referencyjnego w kolumnie Col1 przy użyciu OpenPyxl i Python, tj.
1234 30.5
2334 17.8
4567 9.8
5678 18.3
Po kilku fałszywych uruchomieniach mam to:
#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)
Ale wynik jest kumulatywny i nie usuwa zduplikowanych numerów RFC:
Col1 Col2
1234 12.5
1234 20.7
1234 30.5
2334 40.6
4567 48.3
5678 58.1
itp Nie jestem programistą i szukam sposobu na zaoszczędzenie dużo czasu na edytowaniu dużego arkusza kalkulacyjnego. Wszelkie sugestie są mile widziane. Dziękujemy.
Odpowiedzi:
0 dla odpowiedzi № 1Wypróbuj ten kod:
# 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]))
Testowane z Pythonem: 3.4.2 - openpyxl: 2.4.1 - LibreOffice: 4.3.3.2