简体   繁体   English

之后使用openpyxl将数据写入excel中的列

[英]writing data to columns in excel with openpyxl after

Back again... So now, I've been able to get my data pulled into python with openpyxl and have the calculations completed, but now I'm stuck on how to get it printed back out to another sheet. 再次返回...所以现在,我已经能够使用openpyxl将数据提取到python中,并完成了计算,但是现在我仍然在如何将数据打印回另一张纸上。 The below only prints the last gross value to the referenced cell, which obviously is not what I would like my outcome to be. 下面仅将最后的总值打印到所引用的单元格,这显然不是我希望得到的结果。 I need the row increased and the gross values printed to the excel file. 我需要增加行并将总值打印到excel文件中。 in B2, B3, etc... Referencing the "gross" back with the name of the individual, which is in my dictionaries, and printing that to the third sheet would be even better, so names in A1:Ax and gross B1:Bx. 在B2,B3等中……在我的词典中用个人的名字回“粗体”,然后将其打印到第三张纸上会更好,所以在A1:Ax和总B1中的名字是: Bx。 Help getting to either would be greatly appreciated! 帮助到达任何一个将不胜感激! Thanks 谢谢

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')
#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])))       
        print(format(gross,'.2f'))
        sheet3.cell(row=2,column=2).value=gross

wb.save("Test_book.xlsx")

Personally, I think this really should be done in Excel itself, using a function like vlookup. 就我个人而言,我认为这确实应该在Excel本身中使用vlookup之类的功能完成。 Either way, all you really need to do is keep track of what row you are on 无论哪种方式,您真正需要做的就是跟踪您所在的行

...
sheet3=wb.get_sheet_by_name('payable')
#pulls key from dictionary, multiples it by other number
rownum = 2
for row in sheet['A']:
    if row.value in my_dict:
        gross=(float(my_dict[row.value]*float(my_dict2[row.value])))       
        print(format(gross,'.2f'))
        #put name in A
        sheet3.cell(row=rownum,column=1).value=row.value
        #put gross pay in B
        sheet3.cell(row=rownum,column=2).value=gross
        rownum += 1

wb.save("Test_book.xlsx")

Additionally, openpyxl supports array-index style cell referencing, which can make things easier to read/write: 此外,openpyxl支持数组索引样式的单元格引用,这可以使事情更易于阅读/编写:

#these are equivalent
sheet.cell(row=1, column=1).value = value
sheet.['A1'] = value

You may also at some point find the function openpyxl.utils.get_column_letter() useful. 您有时可能还会发现函数openpyxl.utils.get_column_letter()有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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