Nov 19, 2022

# Solution 1 :

You should divide your table into three logical parts:

2. the ‘central’ content
3. last row

This is the code:

``````import plotly.graph_objects as go
import pandas as pd

data = [['Jan-20', 1.0, 1, 4.0, 0, 250000.0],
['Jan-20', 2.0, 2, 4.0, 0, 250000.0],
['Jan-20', 3.0, 3, 4.0, 0, 250000.0],
['Jan-20', 3.0, 3, 4.0, 0, 250000.0],
['Jan-20', 3.0, 3, 4.0, 0, 250000.0],
['Jan-20', 3.0, 3, 4.0, 0, 250000.0],
['Jan-20', 4.0, 4, 4.0, 0, 250000.0],
['Jan-20', 5, 232, 114, 2312, 123]]
index = ['NL', 'DE', 'AT', 'FR', 'BE', 'ES', 'IT', 'Global']
columns = ['Months', 'budget_rev', 'budget_net_rev', 'gross_margin', 'new_net_mrr', 'act_shipments']
df = pd.DataFrame(data, index=index, columns=columns)

df.reset_index(inplace=True)
n_cols = len(columns)
n_rows = len(data)

rows_c = ['white']*(n_rows-1)

rows_c.extend(['dodgerblue'])

rows_f = ['darkslategray']*(n_rows-1)
rows_f.extend(['white'])

fig = go.Figure(data=[go.Table(columnwidth = [80]*(n_cols+1),
line_color='darkslategray',
font=dict(color='white',
size=12)),
cells=dict(values=list(df.values.transpose()),
line_color='darkslategray',
fill_color = [rows_c*n_cols],
font = dict(color = [rows_f*n_cols],
size = 12)))])

fig.show()
``````

and you get:

# Problem :

I have a DataFrame, and I want to paste it as PNG/JPEG in a Powerpoint.
I want the table to have some formatting. I have been trying with Matplotlib, and plotly, but I am not getting closer to the end result.

I have tried:

``````import plotly.graph_objects as go
import pandas as pd

df = full_table

fig = go.Figure(data=[go.Table(

align='left',
line_color='darkslategray',
font=dict(color='white', size=11)),

cells=dict(values=[df.index, df.budget_revenue, df.budget_net_revenue, df.gross_margin,df.new_net_mrr,df.actual_shipments],
fill_color=['white'],
align='left', line_color='darkslategray', font=dict(color='black', size=11)))
])

fig.write_image("fig1.png")
fig.show()
``````

And I have tried:

``````import matplotlib.pyplot as plt
# Prepare table

df = full_table
hc = '#1D97FF'

columns=full_table.columns.to_list()
index=full_table.index.to_list()
values=full_table.values.tolist()
# Add a table at the bottom of the axes
colors = [["#56b5fd","w","w","w","w","w","w"],[ "#1ac3f5","w","w","w","w","w","w"]]

# Add a table at the bottom of the axes
colors = [["w","w","w","w","w","w"],[ "w","w","w","w","w","w"],
[ "w","w","w","w","w","w"],[ "w","w","w","w","w","w"],
[ "w","w","w","w","w","w"],[ "w","w","w","w","w","w"],
[ "w","w","w","w","w","w"],[ hc,hc,hc,hc,hc,hc]]
colors1 =  ["w","w","w","w","w","w","w",hc]
colors2 = [ hc,hc,hc,hc,hc,hc]

fig, ax = plt.subplots()

ax.axis('off')
the_table = ax.table(cellText=values,cellColours=colors,
colLabels=columns,colColours=colors2,rowLabels=index, rowColours=colors1,loc='center')

plt.show()
``````

The first option looks already quite okay. Some minor changes, but I cant get the bottom row to become blue. And the column headers do not fit.
The second option is blurry, top left corner remains empty, and much more editing of fonts should be done which I did not succeed with either.

Am I just using not the right libraries. Stuck on some simple formatting that is done with Excel in a minute.

This is how it should look like:

### Comment posted by SMP

[[‘Jan-20’, 80000.0, nan, 4.0, nan, 250000.0], [‘Jan-20’, 80000.0, nan, 4.0, nan, 250000.0], [‘Jan-20’, 80000.0, nan, 4.0, nan, 250000.0], [‘Jan-20’, 80000.0, nan, 4.0, nan, 250000.0], [‘Jan-20’, 80000.0, nan, 4.0, nan, 250000.0], [‘Jan-20’, 80000.0, nan, 4.0, nan, 250000.0], [‘Jan-20’, 80000.0, nan, 4.0, nan, 250000.0], [‘Jan-20’, 82854.06800000001, 82854.06800000001, nan, nan, 250000.0]] [‘NL’, ‘DE’, ‘AT’, ‘FR’, ‘BE’, ‘ES’, ‘IT’, ‘Global’] [‘Months’, ‘budget_revenue’, ‘budget_net_revenue’, ‘gross_margin’, ‘new_net_mrr’, ‘actual_shipments’]

### Comment posted by SMP

data = [[‘Jan-20’, 80000.0, 0, 4.0, 0, 250000.0], [‘Jan-20’, 80000.0, 0, 4.0, 0, 250000.0], [‘Jan-20’, 80000.0, 0, 4.0, 0, 250000.0], [‘Jan-20’, 80000.0, 0, 4.0, 0, 250000.0], [‘Jan-20’, 80000.0, 0, 4.0, 0, 250000.0], [‘Jan-20’, 80000.0, 0, 4.0, 0, 250000.0], [‘Jan-20’, 80000.0, 0, 4.0, 0, 250000.0], [‘Jan-20’, 82854.06800000001, 82854.06800000001, 0, 0, 250000.0]] index = [‘NL’, ‘DE’, ‘AT’, ‘FR’, ‘BE’, ‘ES’, ‘IT’, ‘Global’] columns = [‘Months’, ‘budget_revenue’, ‘budget_net_revenue’, ‘gross_margin’, ‘new_net_mrr’, ‘actual_shipments’] df = pd.DataFrame(data, index=index, columns=columns)

### Comment posted by SMP

Looks messy now, import pandas as pd, and there is the dataframe. The values are a little different than the picture above.

### Comment posted by SMP

I just need that formatting. I would like to know what the best way is to do any formatting on a dataframe..

### Comment posted by SMP

That is awesome. I can work with that. Very much appreciated man!!