I have a dataframe which looks like this.
ID | Name | Sales | Sales2 |
---|---|---|---|
111 | SAM | xxx | xxx |
123 | JON | xxx | xxx |
I want to produce an excel like this.
I tried using set tables but it is not working
styles = [{ 'props': ('background-color: green') }]
fd_final_highlight = fd_final.style.set_table_styles(styles)
Weekly_excel= pd.ExcelWriter('C:/Users/xxx.....xlsx', engine='xlsxwriter')
fd_final_highlight.to_excel(Weekly_excel, sheet_name='Master', index=0)
Weekly_excel.save()
CodePudding user response:
Possible solution is following:
import pandas as pd
data = {"col1": [1, 2, 3], "col2": [1, 2, 3]}
df = pd.DataFrame.from_dict(data)
writer = pd.ExcelWriter('C:/Users/xxx.....xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Master', startrow=1, header=False, index=False)
workbook = writer.book
worksheet = writer.sheets['Master']
# Set a header format
header_format = workbook.add_format({'bold': True, 'fg_color': 'green'})
for col_num, value in enumerate(df.columns.values):
worksheet.write(0, col_num, value, header_format)
writer.save()
CodePudding user response:
this is the answer:
df.style.set_table_styles(
[{'selector': 'th.col_heading',
'props': [('background-color', 'yellow')]}])
In your code
styles = [{'selector': 'th.col_heading',
'props': [('background-color', 'yellow')]}]
fd_final.style.set_table_styles(styles)
fd_final.to_excel(<your_path>, sheet_name='Master', index=0)