Skip to content

Instantly share code, notes, and snippets.

@StefanoGITA
Last active July 14, 2021 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StefanoGITA/858ead92186d438f7fa03c5a8329a2a3 to your computer and use it in GitHub Desktop.
Save StefanoGITA/858ead92186d438f7fa03c5a8329a2a3 to your computer and use it in GitHub Desktop.
python
from openpyxl import load_workbook
from openpyxl.utils.dataframe import dataframe_to_rows
template_fout = "<the name of empty excel file to use as template>"
fout_name = "<the name of excel file where save the data>"
...
...
...
# df_values is the DataFrame with the data to save
wb = load_workbook(fout_name)
sheet = wb.active
df_columns = df_values.shape[1]
# I use 2 with enumerate to save the header column of the excel sheet
for r, row in enumerate(dataframe_to_rows(df_values, index=False, header=False), 2):
for c in range(0, df_columns):
sheet.cell(row=r, column=c + 1).value = row[c]
wb.save(fout_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment