Format output Excel - Python

Asked

Viewed 2,259 times

4

I turn a list into a Dataframe and send it to an Excel, it’s working fine. Follow the code:

Df = pd.DataFrame(Lista, columns = colunas_geradas)

writer = pd.ExcelWriter('Exemplo.xlsx', engine = 'xlsxwriter')
Df.to_excel(writer, sheet_name = 'Teste', index = False)
writer.save()

I wonder if it is possible to format Excel in the code. For example: Change the color of the Column Names, tidy up the size of the ballots, add an image, things like that.

2 answers

2


The pandas library has a guide on how to work with styles, but warn that it is a new feature, implemented from version 0.17.1.

In this link I provided you can find snippets of code to paint, create graphics, change colors of the contents of cells, among others.


Change color of column names: I believe it is not possible.

Fix the size of cells: believe that not, however, found this topic teaching an alternative to changing the size of cells using (more or less) the pandas library.

Add an image: is possible using xlsxwriter as shown in this topic.


Besides pandas library, I know two other libraries that allow you to manipulate spreadsheets:

And the library openpyxl, that also has a topic just to talk about how to work with pandas and a topic teaching a little about how to work with styles.


In addition to these libraries, I leave this link that I found extremely informative: Improving Pandas Excel Output.

0

Yes, it has shapes, I usually format them like this:

from win32com.client import Dispatch                           
xl = Dispatch('Excel.Application')
xlsPath = ('diretório do arquivo.xlxs')
wb = xl.Workbooks.open(absPath)
ws = wb.Worksheets(1).Select()
ws = wb.Worksheets(1)

ws.Range("A1").Font.Bold = True #Fonte em Negrito
ws.Range("A1:AF200").Font.Size = 10 #Tamanho da Fonte das células informadas
ws.Range("A1:G1").Merge(False) #Mesclar as células
ws.Range("A1",letra_total_coluna + "1").Interior.ColorIndex = 15 #Colorir o interior das celulas

Browser other questions tagged

You are not signed in. Login or sign up in order to post.