0
import pandas as pd
from openpyxl import load_workbook
df = pd.DataFrame(data={'Dados1': ['Ola','tudo','bom']})
#carrego o Excel com o template pré-formatado 'template.xlsx'
book = load_workbook('template.xlsx')
#defino o writer para escrever em um novo arquivo 'arquivo_editado.xlsx'
writer = pd.ExcelWriter('arquivo_editado.xlsx', engine='openpyxl')
#incluo a formatação no writer
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
#Escrevo com o .to_excel() do pandas
df.to_excel(writer,'Sheet1')
# para escrever só os valores em um lugar específico:
df.to_excel(writer, 'Sheet1', startrow=1, startcol=1, header=False, index=False)
writer.save()
Sources:
Perfect! I had already seen this second link you sent, but I was not able to adapt to this exact problem. Thank you!
– user3701124
Unfortunately the pandas Uga a few edges (some contours just disappear)... I think I’ll have to fix them hard-coded even, but your answer has already made 95% of what I needed.
– user3701124
Excellent, but my file has several tabs, I wanted to put the data of the dataframe in a specific tab, has as?
– guilherme
In function
df.to_excel()
, I believe it’s just changing the string'Sheet1'
by the name of the tab.– AlexCiuffa
I made that attempt, it didn’t work
– guilherme