Test_book.xlsxは従業員の名前、時間、料金、合計支払い額を表示する既存のExcelファイルです。従業員の名前と時間を1枚のシートに表示し、従業員の名前と支払いレートを2枚目のシートに表示し、最終的に従業員の名前と最後のシートに支払額を表示する部分に移動しました。しかし、私はどのように各シート(従業員の名前、時間、時間別料金、合計支払い)に列名を置くかを把握していないようです。あなたがこの添付画像を通して見ることができるように、そこには列が存在しない既存のExcelスプレッドシートに列名をどのように置くのですか?
import openpyxl
from itertools import chain
from collections import defaultdict
wb = openpyxl.load_workbook("Test_book.xlsx")
sheet=wb.get_sheet_by_name('Hours')
employee_names=[]
employee_hours=[]
for row in sheet['A']:
employee_names.append(row.value)
for row in sheet['B']:
employee_hours.append(row.value)
print(employee_names)
print(employee_hours)
my_dict=dict(zip(employee_names,employee_hours))
print(my_dict)
sheet2=wb.get_sheet_by_name('Rate')
employee_names2=[]
employee_Rate=[]
for row in sheet2['A']:
employee_names2.append(row.value)
for row in sheet2['B']:
employee_Rate.append(row.value)
print(employee_names2)
print(employee_Rate)
my_dict2=dict(zip(employee_names2,employee_Rate))
print(my_dict2)
sheet3=wb.get_sheet_by_name('payable')
sheet3rows=[]
#pulls key from dictionary, multiples it by other number
for row in sheet['A']:
if row.value in my_dict:
gross=(float(my_dict[row.value]*float(my_dict2[row.value])))
#creates list
sheet3rows.append(gross)
print(format(gross,'.2f'))
print(sheet3rows)
my_dict3=dict(zip(employee_names2,sheet3rows))
print(my_dict3)
#Print gross to payable sheet
r=1
for x in sheet3rows:
sheet3.cell(row=r,column=2).value=x
r+=1
#wb.save("Test_book.xlsx")
my_dict4 = defaultdict(list)
for k, v in chain(my_dict.items(), my_dict2.items(), my_dict3.items()):
my_dict4[k].append(v)
for k, v in my_dict4.items():
print(k, v)
あなたはパンダを使用して考えがありますか?私はこれのすべてではないにしても、ほとんどが5行未満で実行できると思います(例えば、read_excel、merge、ExcelWriterなど)。 – DrTRD
私は現時点でパンダを使用できません。私は現在持っているものの上に追加のコーディングをすることでそれを行う方法はありますか?ありがとうございました。 –