Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

364
Views
How can I dynamically add a total to the end of the row in Excel with Python?
for item in results:    
    titleformat = book.add_format({'bold': True, 'align': 'center'})
    center      = book.add_format({'align' : 'center'})

    usersheet.write(0, 0, "#"             , titleformat)
    usersheet.write(0, 1, "User"          , titleformat)
    usersheet.write(0, 2, "Group"         , titleformat)
    usersheet.write(0, 3, "SVN Commits"   , titleformat)
    usersheet.write(0, 4, "GIT Commits"   , titleformat)
    usersheet.write(0, 5, "GITLAB Commits", titleformat)
    usersheet.write(0, 6, "Gerrit Review" , titleformat)
.
.
.
.
    #### set column widths
    usersheet.set_column(userrowno, 0, 5)
    for i in range(1,26):
        usersheet.set_column(userrowno, i, 20)

    
    userrowno = userrowno + 1

With this piece of code, we dynamically create two Excel sheets. However, because the data is always changing, the last row looks different for each data. How can I dynamically write the total data at the end of each column in Python?The TOTAL text in the picture should be generated as generic in the code and should be written at the end of each column.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The easiest way would be to read this into a dataframe in pandas, sum on columns, then rewrite your sheet with the titles.

import pandas as pd

df = pd.read_excel(path_to_sheet)
df.loc["Total"] = df.sum()
writer = pd.ExcelWriter(path_where_you_want)

...do what you're doing above with pandas writer object...

df.to_excel(writer)

https://pandas.pydata.org/docs/reference/api/pandas.ExcelWriter.html

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_excel.html

Edit: you may need to set your index if you have non-numeric columns

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!