XlsxWriter: add color to cells - python

I try to write dataframe to xlsx and give color to that.
I use
worksheet.conditional_format('A1:C1', {'type': '3_color_scale'})
But it's not give color to cell. And I want to one color to this cells.
I saw cell_format.set_font_color('#FF0000')
but there is don't specify number of cells
sex = pd.concat([df2[["All"]],df3], axis=1)
excel_file = 'example.xlsx'
sheet_name = 'Sheet1'
writer = pd.ExcelWriter(excel_file, engine='xlsxwriter')
sex.to_excel(writer, sheet_name=sheet_name, startrow=1)
workbook = writer.book
worksheet = writer.sheets[sheet_name]
format = workbook.add_format()
format.set_pattern(1)
format.set_bg_color('gray')
worksheet.write('A1:C1', 'Ray', format)
writer.save()
I need to give color to A1:C1, but I should give name to cell. How can I paint several cells of my df?

The problem is that worksheet.write('A1:C1', 'Ray', format) is used only to write a single cell.
A possible solution to write more cells in a row, is use write_row().
worksheet.write_row("A1:C1", ['Ray','Ray2','Ray3'], format)
Remember that write_row() takes a list of string to write in cells.
If you use worksheet.write_row("A1:C1", 'Ray', format), you have R in the first cell, a in second and y in the third.

cf = workbook.add_format({'bg_color': 'yellow'})
worksheet.write('A1', 'Column name', cf)

Related

XLSXWriter - Center Alignment doesn't work with set_column using a Format object

I have been trying to format my Excel Output by center-aligning horizontally and vertically my columns. However, when checking the exported xlsx file, everything is still left-aligned. One explanation I found is :
In XlsxWriter, as in Excel, a cell format overrides a column format.
Do I need to go over every one of my cells to apply the desired format to it ?
Thank you for your help !
writer = pd.ExcelWriter("Analyse PRM.xlsx", engine='xlsxwriter')
analyse_prm.style.apply(highlight_prm, axis = None).to_excel(writer, sheet_name='Cohérence PRM DR - PIX-F', index=False)
workbook = writer.book
worksheet = writer.sheets['Cohérence PRM DR - PIX-F']
worksheet.set_zoom(90)
max_row = len(analyse_pixf.index)
max_col = len(analyse_pixf.columns)
worksheet.autofilter(0, 0, max_row, max_col - 1)
cell_format = workbook.add_format()
cell_format.set_align('center')
cell_format.set_align('vcenter')
#Column width & format
worksheet.set_column('A:J',20, cell_format)
writer.save()

Force pandas.to_excel to write data as 'Text' instead 'General' format

I've already set cell format as 'Text' in the target Excel column. However pandas.to_excel changes the format to 'General' when writing strings to this column, eventually the column ends up with blank cells are formatted as 'Text' and non-blank ones as 'General'. Is there a way to write data as 'Text' instead of 'General'?
def exportData(df, dstfile, sheet):
book = load_workbook(dstfile)
writer = pd.ExcelWriter(dstfile, engine='openpyxl', date_format='dd/mm/yyyy', datetime_format='mm/dd/yyyy hh:mm')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
df.to_excel(writer, header=False, startrow=2, index=False, sheet_name=sheet)
writer.save()
You can iterate over the cells using the following method.
The cells you want to format as TEXT just use:
cell.number_format = '#'
This will set the cell formatting as TEXT in Excel.
There might be a way to do it straight from the ExcelWriter within Pandas but I'm unfarmiliar with it, maybe someone who knows better will edit the answer for that option as well.
All you need to do is to save data in pandas as 'object' (which is text) like:
df = pd.DataFrame(data=d, dtype=object)
and after that if you export to Excel with pandas to_excel method it will be stored in Excel as text.

How to color text in a cell containing a specific string using pandas

After running my algorithms I saved all the data in an excel file using pandas.
writer = pd.ExcelWriter('Diff.xlsx', engine='xlsxwriter')
Now, some of the cells contain strings which includes "-->" in it. I have the row and column number for those cells using:
xl_rowcol_to_cell(rows[i],cols[i])
But I couldn't figure how to color those cells or atleast the whole text in it.
Any suggestions/tips?
You could use a conditional format in Excel like this:
import pandas as pd
# Create a Pandas dataframe from some data.
df = pd.DataFrame({'Data': ['foo', 'a --> b', 'bar']})
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_conditional.xlsx', engine='xlsxwriter')
# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1')
# Get the xlsxwriter workbook and worksheet objects.
workbook = writer.book
worksheet = writer.sheets['Sheet1']
# Add a format. Light red fill with dark red text.
format1 = workbook.add_format({'bg_color': '#FFC7CE',
'font_color': '#9C0006'})
# Apply a conditional format to the cell range.
worksheet.conditional_format(1, 1, len(df), 1,
{'type': 'text',
'criteria': 'containing',
'value': '-->',
'format': format1})
# Close the Pandas Excel writer and output the Excel file.
writer.save()
Output:
See Adding Conditional Formatting to Dataframe output in the XlsxWriter docs.
def highlight (dataframe):
if dataframe[dataframe['c'].str.contains("-->")]:
return ['background-color: yellow']*5
else:
return ['background-color: white']*5
df.style.apply(highlight, axis=1)

Pandas DataFrame to Excel: Vertical Alignment of Index

Given the following data frame:
import pandas as pd
d=pd.DataFrame({'a':['a','a','b','b'],
'b':['a','b','c','d'],
'c':[1,2,3,4]})
d=d.groupby(['a','b']).sum()
d
I'd like to export this with the same alignment with respect to the index (see how the left-most column is centered vertically?).
The rub is that when exporting this to Excel, the left column is aligned to the top of each cell:
writer = pd.ExcelWriter('pandas_out.xlsx', engine='xlsxwriter')
workbook = writer.book
f=workbook.add_format({'align': 'vcenter'})
d.to_excel(writer, sheet_name='Sheet1')
writer.save()
...produces...
Is there any way to center column A vertically via XLSX Writer or another library?
Thanks in advance!
You are trying to change the formatting of the header so you should first reset the default header settings
from pandas.io.formats.excel import ExcelFormatter
ExcelFormatter.header_style = None
Then apply the formatting as required
format = workbook.add_format()
format.set_align('center')
format.set_align('vcenter')
worksheet.set_column('A:C',5, format)
here is complete working code
d=pd.DataFrame({'a':['a','a','b','b'],
'b':['a','b','c','d'],
'c':[1,2,3,4]})
d=d.groupby(['a','b']).sum()
pd.core.format.header_style = None
writer = pd.ExcelWriter('pandas_out.xlsx', engine='xlsxwriter')
workbook = writer.book
d.to_excel(writer, sheet_name='Sheet1')
worksheet = writer.sheets['Sheet1']
format = workbook.add_format()
format.set_align('center')
format.set_align('vcenter')
worksheet.set_column('A:C',5, format)
writer.save()

Xlsxwriter - Trouble formatting pandas dataframe cells using xlsxwriter

I have an excel sheet which came from a pandas dataframe. I then use Xlsxwriter to add formulas, new columns and formatting. The problem is I only seem to be able format what I've written using xlsxwriter and nothing that came from the dataframe. So what I get is something like this half formatted table
As you can see from the image the two columns from the dataframe remain untouched. They must have some kind of default formatting that is overriding mine.
Since I don't know how to convert a worksheet back into to a dataframe the code below is obviously completely wrong but it's just to give an idea of what I'm looking for.
export = "files/sharepointExtract.xlsx"
df = pd.read_excel(export)# df = dataframe
writer = pd.ExcelWriter('files/new_report-%s.xlsx' % (date.today()), engine = 'xlsxwriter')
workbook = writer.book
# Code to make the header red, this works fine because
# it's written in xlsxwriter using write.row()
colour_format = workbook.add_format()
colour_format.set_bg_color('#640000')
colour_format.set_font_color('white')
worksheet.set_row(0, 15, colour_format)
table_body_format = workbook.add_format()
table_body_format.set_bg_color('blue')
for row in worksheet.rows:
row.set_row(0,15, table_body_format)
This code gives an Attribute error but even without the for loop we just get what can be seen in the image.
The following should work:
import pandas as pd
from datetime import date
export = "files/sharepointExtract.xlsx"
df = pd.read_excel(export)
writer = pd.ExcelWriter('files/new_report-{}.xlsx'.format(date.today()), engine ='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', startrow=1 , startcol=0, header=False, index=False, encoding='utf8')
workbook = writer.book
worksheet = writer.sheets['Sheet1']
# Code to make the header red background with white text
colour_format = workbook.add_format()
colour_format.set_bg_color('#640000')
colour_format.set_font_color('white')
# Code to make the body blue
table_body_format = workbook.add_format()
table_body_format.set_bg_color('blue')
# Set the header (row 0) to height 15 using colour_format
worksheet.set_row(0, 15, colour_format)
# Set the default format for other rows
worksheet.set_column('A:Z', 15, table_body_format)
# Write the header manually
for colx, value in enumerate(df.columns.values):
worksheet.write(0, colx, value)
writer.save()
When Pandas is used to write the header, it uses its own format style which overwrites the underlying xlsxwriter version. The simplest approach is to stop it from writing the header and get it to write the rest of the data from row 1 onwards (not 0). This avoids the formatting from being altered. You can then easily write your own header using the column values from the dataframe.

Categories

Resources