Test_book.xlsx는 직원 이름, 시간, 요율 및 총 지불액을 표시해야하는 기존 Excel 파일입니다. 직원 이름과 근무 시간을 한 시트에 표시하고 직원 이름과 지불 비율을 두 번째 시트에 표시하고 마지막으로 직원 이름과 마지막 결제일을 마지막 시트에 표시하는 부분에 도달했습니다. 그러나 각 시트 (직원 이름, 시간, 시간당 요금, 총 지불)에 열 이름을 지정하는 방법을 알아낼 수 없습니다.기존 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
현재 팬더를 사용할 수 없습니다. 현재 가지고있는 코드 위에 추가 코딩 기능을 추가 할 수 있습니까? 고맙습니다. –