Skip to content

Instantly share code, notes, and snippets.

@arkottke
Created May 15, 2014 16:43
Show Gist options
  • Save arkottke/dd1da34cb602512db0aa to your computer and use it in GitHub Desktop.
Save arkottke/dd1da34cb602512db0aa to your computer and use it in GitHub Desktop.
Standardize Excel charts to something a little more attractive.
#!/usr/bin/env python3
# encoding: utf-8
import re
import win32com.client
xl = win32com.client.gencache.EnsureDispatch('Excel.Application')
wb = xl.ActiveWorkbook
if wb is None:
raise RuntimeError('No active workbooks')
chart = wb.ActiveChart
if chart is None:
raise RuntimeError('No active chart')
# Change the color of the tab to Green
chart.Tab.Color = 5880731
# Remove the border around chart
chart.ChartArea.RoundedCorners = False
chart.ChartArea.Format.Line.Visible = False
# Add border and grid to plot area
chart.PlotArea.Border.ColorIndex = 16
for ax in chart.Axes():
ax.HasMajorGridlines = True
ax.HasMinorGridlines = True
if ax.HasTitle:
ax.AxisTitle.Font.Name = 'Arial'
ax.AxisTitle.Font.Size = 12
ax.AxisTitle.Font.Bold = True
ax.TickLabels.Font.Name = 'Arial'
ax.TickLabels.Font.Size = 11
ax.TickLabels.Font.Bold = False
if chart.HasTitle:
chart.ChartTitle.Font.Name = 'Arial'
chart.ChartTitle.Font.Size = 14
chart.ChartTitle.Font.Bold = True
if chart.HasLegend:
chart.Legend.Format.Fill.ForeColor.RGB = 16777215 # White
chart.Legend.Format.Line.ForeColor.RGB = 8355711 # Grey
chart.Legend.Font.Name = 'Arial'
chart.Legend.Font.Size = 12
# Use a fixed position for the frame
chart.PlotArea.Top = 6
chart.PlotArea.Left = 20
chart.PlotArea.Height = 475
chart.PlotArea.Width = 650
for series in chart.SeriesCollection():
series.Format.Line.Weight = 3
# Remove un-named series from the legend
if chart.HasLegend:
for i in range(chart.Legend.LegendEntries().Count):
entry = chart.Legend.LegendEntries(i + 1)
name = chart.SeriesCollection(entry.Index).Name
if re.match(r'Series\d+', name):
# Remove the unnamed entry
entry.Delete()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment