2

I have a pandas dataframe and I want to open an existing excel workbook containing formulas, copying the dataframe in a specific set of columns (lets say from column A to column H) and save it as a new file with a different name.

The idea is to update an existing template, populate it with the dataframe in a specified set of column and then save a copy of the Excel file with a different name.

Any idea?

What I have is:

  import pandas
  from openpyxl import load_workbook

 book = load_workbook('Template.xlsx')
 writer = pandas.ExcelWriter('Template.xlsx', engine='openpyxl') 
 writer.book = book
 writer.sheets = dict((ws.title, ws) for ws in book.worksheets)

 df.to_excel(writer)

 writer.save()
3
  • and what is wrong with your code? Commented Mar 11, 2016 at 13:32
  • i would suggest first make a copy of the 'Template.xlsx' file and then work with that copy instead of "saving with different name" Commented Mar 11, 2016 at 13:34
  • @MaxU I can't set the starting column! Commented Mar 11, 2016 at 14:50

2 Answers 2

4

The below should work, assuming that you are happy to copy into column A. I don't see a way to write into the sheet starting in a different column (without overwriting anything).

The below incorporates @MaxU's suggestion of copying the template sheet before writing to it (having just lost a few hours' work on my own template workbook to pd.to_excel)

import pandas as pd
from openpyxl.utils.dataframe import dataframe_to_rows
from shutil import copyfile

template_file = 'Template.xlsx' # Has a header in row 1 already
output_file = 'Result.xlsx' # What we are saving the template as

# Copy Template.xlsx as Result.xlsx
copyfile(template_file, output_file)

# Read in the data to be pasted into the termplate
df = pd.read_csv('my_data.csv') 

# Load the workbook and access the sheet we'll paste into
wb = load_workbook(output_file)
ws = wb.get_sheet_by_name('Existing Result Sheet') 

# Selecting a cell in the header row before writing makes append()
#  start writing to the following line i.e. row 2
ws['A1']
# Write each row of the DataFrame
# In this case, I don't want to write the index (useless) or the header (already in the template)
for r in dataframe_to_rows(df, index=False, header=False):
    ws.append(r)

wb.save(output_file)
Sign up to request clarification or add additional context in comments.

1 Comment

Here's the answer for starting in a particular column: stackoverflow.com/questions/36657288/… Replace the for loop in the above with the nested for loop in the linked example.
0

try this:

df.to_excel(writer, startrow=10, startcol=1, index=False, engine='openpyxl')

Pay attention at startrow and startcol parameters

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.