Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add borders to a table in excel sheet created by pandas dataframe?

import pandas as pd
table =[[1,2,3,4],[11,12,13,14],["Pass","Fail","Pass","Fail"]]
df = pd.DataFrame(table)
df = df.transpose()
headers=["Current_Value","Previous_Value","Result",]
df.columns =headers

writer = pd.ExcelWriter("pandas.xlsx")

df.to_excel(writer, sheet_name='Sheet1')
writer.save()

This code will create a table with headers in bold. I want to add borders to the table that is present in the excel sheet. Will that be possible?

like image 582
user1681102 Avatar asked Aug 03 '17 20:08

user1681102


People also ask

How do I insert a data frame into an Excel spreadsheet?

Use pandas to_excel() function to write a DataFrame to an excel sheet with extension . xlsx. By default it writes a single DataFrame to an excel file, you can also write multiple sheets by using an ExcelWriter object with a target file name, and sheet name to write to.

How do you display a data frame as a table?

Example 1 : One way to display a dataframe in the form of a table is by using the display() function of IPython. display .

How do I write pandas DataFrames to an existing Excel spreadsheet?

You can write any data (lists, strings, numbers etc) to Excel, by first converting it into a Pandas DataFrame and then writing the DataFrame to Excel. To export a Pandas DataFrame as an Excel file (extension: . xlsx, . xls), use the to_excel() method.


1 Answers

You can use StyleFrame to add Borders/Colors to your Dataframe in Excel.

import pandas as pd
from StyleFrame import StyleFrame, Styler, utils

table =[[1,2,3,4],[11,12,13,14],["Pass","Fail","Pass","Fail"]]
df = pd.DataFrame(table)
df = df.transpose()
headers=["Current_Value","Previous_Value","Result",]
df.columns =headers

writer = StyleFrame.ExcelWriter("pandas.xlsx")

sf=StyleFrame(df)

sf.apply_column_style(cols_to_style=df.columns, styler_obj=Styler(bg_color=utils.colors.white, bold=True, font=utils.fonts.arial,font_size=8),style_header=True)

sf.apply_headers_style(styler_obj=Styler(bg_color=utils.colors.blue, bold=True, font_size=8, font_color=utils.colors.white,number_format=utils.number_formats.general, protection=False))

sf.to_excel(writer, sheet_name='Sheet1')
writer.save()
like image 186
Marimuthukumar Madhimannan Avatar answered Sep 25 '22 17:09

Marimuthukumar Madhimannan