简体   繁体   中英

How do you put column names on existing excel spreadsheet through python?

Test_book.xlsx is an existing excel file that is supposed to display employee names, hours, rate, and total payment. I got to a part where it displays employee names and hours in one sheet, employee names and payment rate on the second sheet, and finally displaying employee names and total payment on the last sheet; however, I can't seem to figure out how to put column names on each sheets (Employee Names, Hours, Hourly Rates, Total Payment). Here is the code I've been working on:

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)        

As you can see through this attached image, there are no columns there: 在此处输入图片说明

To add column name to an existing sheet in an excel document, you will have to insert a row at the top of the sheet. There are 2 big problems with that:

  • Inserting a row is not difficult, but apparently it has not (or not yet) been implemented in openpyxl.
  • Inserting a row will change the contents of the sheet. Some programs might rely on the name of the first employee being in cell A1 of the Payable sheet.

Anyway: If you really want to do that to the Payable Sheet, you can! You have got all the relevant information in the my_dict3 dictionary.

  1. So you can erase all contents from the Payable Sheet.

  2. After that, you can simply write the column headers:

     sheet3.cell(row=1, column=1).value = 'Employee Name' sheet3.cell(row=1, column=2).value = 'Total Payment' 
  3. After that, you can fill the other rows:

     rownum = 2 for (k,v) in my_dict3.iteritems: sheet3.cell(row=rownum, column=1).value = k sheet3.cell(row=rownum, column=2).value = v rownum = rownum + 1 
  4. Please make sure that there's no old data in the remaining rows of the sheet ...

As far as I understood, the payable sheet is your output sheet (your program does not write anything to the other 2 sheets).

Still, you should only do this if you are really sure that it's OK to throw away the existing information on that sheet.

(Writing header rows to the first 2 sheets might not be a good idea, since you are using them as input only.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM