Chart style unifier

From aipoch/medical-research-skills · View interactive version

Chart style unifier is an AI agent skill from the GitHub repository aipoch/medical-research-skills. Batch-unify typography (font family, size, italics) for Word table cells and embedded charts; use when you need consistent formatting across theses/reports without changing body text.

How to use this skill

Skill file

Source: https://github.com/aipoch/medical-research-skills

Chart Style Unifier

When to Use

Key Features

Dependencies

Example Usage

cd "20260316/scientific-skills/Others/chart-style-unifier"
python -m py_compile scripts/change_table_font.py
python scripts/change_table_font.py --help

Example run plan:

  1. Confirm the user input, output path, and any required config values.
  2. Edit the in-file CONFIG block or documented parameters if the script uses fixed settings.
  3. Run python scripts/change_table_font.py with the validated inputs.
  4. Review the generated output and return the final artifact with any assumptions called out.

Implementation Details

1. When to Use

2. Key Features

3. Dependencies

| Dependency | Version | Purpose | Notes |

|---|---:|---|---|

| python-docx | >= 0.8.11 | Modify Word table cell runs/fonts | Used by scripts/change_table_font.py |

| pywin32 | >= 306 | Word/Excel COM automation for chart macros | Windows-only; required for COM-based operations |

Installation:

pip install python-docx pywin32

Optional (mirror):

pip install -i https://mirrors.aliyun.com/pypi/simple python-docx pywin32

4. Example Usage

4.1 Unify Word Table Cell Fonts (Python, runnable)


# 1) Initialize a run directory for table processing
python scripts/init_run.py --table

# 2) Edit the generated config:

# outputs/runs/<timestamp>/config.json

# 3) Execute the table font unification
python scripts/change_table_font.py

Example outputs/runs/<timestamp>/config.json:

{
  "target_doc_path": "C:/Users/xxx/Desktop/document.docx",
  "font_name": "SimSun",
  "font_size": 10,
  "target_type": "table",
  "italic_numbers": false,
  "italic_chinese": false,
  "lowercase_letters": false,
  "status": "pending"
}

4.2 Unify Embedded Chart Typography (Word VBA, runnable)

Paste into Word VBA Editor (Alt+F11) and run NormalizeChartTypography:

Option Explicit

Public Sub NormalizeChartTypography()
    Dim fontName As String
    Dim baseSize As Single, titleSize As Single
    Dim axisTitleSize As Single, tickLabelSize As Single
    Dim legendSize As Single, dataLabelSize As Single

    fontName = InputBox("Font Name", "Unify Chart Fonts", "Times New Roman")
    baseSize = CSng(InputBox("Base Size (ChartArea)", "Unify Chart Fonts", "9"))
    titleSize = CSng(InputBox("Chart Title Size", "Unify Chart Fonts", "11"))
    axisTitleSize = CSng(InputBox("Axis Title Size", "Unify Chart Fonts", "9"))
    tickLabelSize = CSng(InputBox("Tick Label Size", "Unify Chart Fonts", "8"))
    legendSize = CSng(InputBox("Legend Size", "Unify Chart Fonts", "10"))
    dataLabelSize = CSng(InputBox("Data Label Size", "Unify Chart Fonts", "8"))

    Application.ScreenUpdating = False

    Dim ish As InlineShape, shp As Shape
    For Each ish In ActiveDocument.InlineShapes
        If ish.HasChart Then
            ApplyChartTypography ish.Chart, fontName, baseSize, titleSize, axisTitleSize, tickLabelSize, legendSize, dataLabelSize
        End If
    Next ish

    For Each shp In ActiveDocument.Shapes
        If shp.HasChart Then
            ApplyChartTypography shp.Chart, fontName, baseSize, titleSize, axisTitleSize, tickLabelSize, legendSize, dataLabelSize
        End If
    Next shp

    Application.ScreenUpdating = True
End Sub

Private Sub ApplyChartTypography(cht As Object, fontName As String, baseSize As Single, _
                                 titleSize As Single, axisTitleSize As Single, tickLabelSize As Single, _
                                 legendSize As Single, dataLabelSize As Single)
    On Error Resume Next

    With cht.ChartArea.Format.TextFrame2.TextRange.Font
        .Name = fontName
        .Size = baseSize
    End With

    If cht.HasTitle Then
        With cht.ChartTitle.Format.TextFrame2.TextRange.Font
            .Name = fontName
            .Size = titleSize
        End With
    End If

    If cht.HasLegend Then
        With cht.Legend.Format.TextFrame2.TextRange.Font
            .Name = fontName
            .Size = legendSize
        End With
    End If

    Dim axType As Variant, axGroup As Variant, ax As Object
    For Each axType In Array(1, 2) ' 1=xlCategory, 2=xlValue
        For Each axGroup In Array(1, 2) ' 1=xlPrimary, 2=xlSecondary
            Set ax = Nothing
            Set ax = cht.Axes(axType, axGroup)
            If Not ax Is Nothing Then
                ax.TickLabels.Font.Name = fontName
                ax.TickLabels.Font.Size = tickLabelSize
                If ax.HasTitle Then
                    ax.AxisTitle.Format.TextFrame2.TextRange.Font.Name = fontName
                    ax.AxisTitle.Format.TextFrame2.TextRange.Font.Size = axisTitleSize
                End If
            End If
        Next axGroup
    Next axType

    Dim s As Object
    For Each s In cht.SeriesCollection
        If s.HasDataLabels Then
            s.DataLabels.Font.Name = fontName
            s.DataLabels.Font.Size = dataLabelSize
        End If
    Next s

    On Error GoTo 0
End Sub

4.3 Unify Linked Chart Source Files (Excel VBA, runnable)

Run in Excel on the source workbook(s) before refreshing links in Word:

Option Explicit

Public Sub NormalizeLinkedChartTypography()
    Dim fontName As String
    Dim baseSize As Single, titleSize As Single
    Dim axisTitleSize As Single, tickLabelSize As Single
    Dim legendSize As Single, dataLabelSize As Single

    fontName = InputBox("Font Name", "Unify Chart Fonts", "Times New Roman")
    baseSize = CSng(InputBox("Base Size (ChartArea)", "Unify Chart Fonts", "9"))
    titleSize = CSng(InputBox("Chart Title Size", "Unify Chart Fonts", "11"))
    axisTitleSize = CSng(InputBox("Axis Title Size", "Unify Chart Fonts", "9"))
    tickLabelSize = CSng(InputBox("Tick Label Size", "Unify Chart Fonts", "8"))
    legendSize = CSng(InputBox("Legend Size", "Unify Chart Fonts", "10"))
    dataLabelSize = CSng(InputBox("Data Label Size", "Unify Chart Fonts", "8"))

    Application.ScreenUpdating = False

    Dim ws As Worksheet, co As ChartObject
    For Each ws In ActiveWorkbook.Worksheets
        For Each co In ws.ChartObjects
            ApplyChartTypography co.Chart, fontName, baseSize, titleSize, axisTitleSize, tickLabelSize, legendSize, dataLabelSize
        Next co
    Next ws

    Application.ScreenUpdating = True
End Sub

5. Implementation Details

5.1 Processing Scope and Constraints

5.2 Python Table Unification Logic (Conceptual)

5.3 Chart Typography Tokens (What Gets Normalized)

Recommended parameter mapping (used by the VBA macros):

| Element | VBA Target | Parameter |

|---|---|---|

| Chart area base | Chart.ChartArea | baseSize |

| Chart title | Chart.ChartTitle | titleSize |

| Axis titles | Axes(...).AxisTitle | axisTitleSize |

| Tick labels | Axes(...).TickLabels | tickLabelSize |

| Legend | Chart.Legend | legendSize |

| Data labels | Series.DataLabels | dataLabelSize |

5.4 Linked Charts: Overwrite Behavior

1. Normalize chart typography in the Excel source files (Excel VBA).

2. Refresh/update links in Word.

3. Only use Word VBA for charts that are truly embedded (not linked).

5.5 Repository Entry Points

| Script | Purpose |

|---|---|

| scripts/init_run.py | Create outputs/runs/<timestamp>/ and a starter config.json |

| scripts/generate_style_pack.py | Generate matplotlib/plotly style packs from a unified config |

| scripts/change_table_font.py | Apply table cell font normalization based on config.json |

5.6 References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.