Last modified: Nov 12, 2025 By Alexander Williams
Add Charts to DOCX with Python Workarounds
Python-docx is great for Word automation. But it has one big limitation. It cannot add charts directly. This article shows you smart workarounds.
Understanding the python-docx Limitation
Many developers discover this problem late. They build complex document systems. Then they hit the chart wall.
The python-docx library handles text well. It manages paragraphs and tables beautifully. But charts are different.
Charts require OLE objects in Word. This is complex Windows functionality. The pure Python library cannot do it.
Method 1: Generate Charts as Images
This is the most popular solution. Create charts using matplotlib. Then insert them as images into your DOCX.
First, install the required libraries. You need python-docx and matplotlib.
pip install python-docx matplotlib
Now create a simple bar chart. Save it as an image file. Then insert it into your document.
import matplotlib.pyplot as plt
from docx import Document
from docx.shared import Inches
# Create sample data
categories = ['Q1', 'Q2', 'Q3', 'Q4']
sales = [12000, 18000, 15000, 22000]
# Create the chart
plt.figure(figsize=(8, 6))
plt.bar(categories, sales, color='skyblue')
plt.title('Quarterly Sales Report')
plt.xlabel('Quarters')
plt.ylabel('Sales ($)')
plt.tight_layout()
# Save chart as image
plt.savefig('sales_chart.png', dpi=300, bbox_inches='tight')
plt.close()
# Create Word document and insert chart
doc = Document()
doc.add_heading('Sales Report with Chart', 0)
doc.add_paragraph('Below is our quarterly sales performance:')
# Add the chart image to document
doc.add_picture('sales_chart.png', width=Inches(6.0))
doc.save('sales_report_with_chart.docx')
The output is a professional Word document. It contains your chart as a high-quality image. Users can view and print it normally.
Method 2: Use Plotly for Interactive Charts
Plotly creates beautiful, modern charts. You can export them as static images. Then add to your Word documents.
Install Plotly for this approach.
pip install plotly kaleido
Kaleido is needed for static image export. Now create an interactive-style chart.
import plotly.graph_objects as go
from docx import Document
from docx.shared import Inches
# Create sample data
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
revenue = [5000, 7000, 6500, 8000, 9500]
expenses = [3000, 3500, 4000, 3800, 4200]
# Create Plotly figure
fig = go.Figure()
fig.add_trace(go.Scatter(x=months, y=revenue,
mode='lines+markers',
name='Revenue',
line=dict(color='green', width=3)))
fig.add_trace(go.Scatter(x=months, y=expenses,
mode='lines+markers',
name='Expenses',
line=dict(color='red', width=3)))
fig.update_layout(title='Monthly Revenue vs Expenses',
xaxis_title='Month',
yaxis_title='Amount ($)')
# Export as image
fig.write_image("financial_chart.png", width=800, height=600, scale=2)
# Add to Word document
doc = Document()
doc.add_heading('Financial Overview', 0)
doc.add_picture('financial_chart.png', width=Inches(6.0))
doc.add_paragraph('This chart shows our financial performance over five months.')
doc.save('financial_report.docx')
Plotly charts look very professional. They work well in business reports. The image quality is excellent for printing.
Method 3: Pre-made Chart Templates
Sometimes you need the same chart format repeatedly. Create template charts in Word. Then use python-docx to update them.
First, create a template document. Add a placeholder chart in Word. Save it as template.docx.
Now use python-docx to manipulate the template. You can update text and data around the chart.
from docx import Document
# Load template with pre-made chart
doc = Document('template.docx')
# Find and replace placeholder text
for paragraph in doc.paragraphs:
if '{{QUARTER}}' in paragraph.text:
paragraph.text = paragraph.text.replace('{{QUARTER}}', 'Q2 2024')
if '{{TOTAL_SALES}}' in paragraph.text:
paragraph.text = paragraph.text.replace('{{TOTAL_SALES}}', '$65,000')
# Save as new document
doc.save('updated_report.docx')
This method works well for standardized reports. You maintain chart formatting in Word. Python handles the data updates.
Method 4: Advanced Workflow with Multiple Tools
For complex requirements, combine multiple approaches. Generate different chart types. Insert them into various document sections.
This example shows a comprehensive report. It uses multiple data visualizations.
import matplotlib.pyplot as plt
import plotly.express as px
import pandas as pd
from docx import Document
from docx.shared import Inches
# Sample data
data = {
'Department': ['Sales', 'Marketing', 'IT', 'HR', 'Finance'],
'Budget': [50000, 30000, 40000, 20000, 35000],
'Actual': [48000, 32000, 38000, 21000, 34000]
}
df = pd.DataFrame(data)
# Create document
doc = Document()
doc.add_heading('Department Budget Analysis', 0)
# Matplotlib bar chart
plt.figure(figsize=(10, 6))
x = range(len(df))
width = 0.35
plt.bar([i - width/2 for i in x], df['Budget'], width, label='Budget', color='blue')
plt.bar([i + width/2 for i in x], df['Actual'], width, label='Actual', color='orange')
plt.xticks(x, df['Department'])
plt.legend()
plt.title('Budget vs Actual by Department')
plt.tight_layout()
plt.savefig('budget_chart.png', dpi=300)
plt.close()
doc.add_heading('Budget Comparison', level=1)
doc.add_picture('budget_chart.png', width=Inches(6.0))
# Plotly pie chart for budget distribution
fig = px.pie(df, values='Budget', names='Department',
title='Budget Distribution Across Departments')
fig.write_image("budget_pie.png", width=600, height=400, scale=2)
doc.add_heading('Budget Distribution', level=1)
doc.add_picture('budget_pie.png', width=Inches(5.0))
doc.save('comprehensive_report.docx')
This creates a rich, multi-chart document. Each visualization serves a different purpose. The report becomes much more valuable.
Best Practices for Chart Integration
Follow these guidelines for best results. They improve your chart documents significantly.
Image quality matters. Use high DPI settings. 300 DPI works well for printing. Balance file size and quality.
Consistent sizing makes documents look professional. Use the Inches class for consistent dimensions.
Add captions and explanations. Charts need context. Use python-docx to add descriptive text.
Consider your audience. Technical readers prefer detailed charts. Executives want high-level summaries.
Alternative: win32com for Native Charts
If you must have native Word charts, consider win32com. This library controls Word directly. But it only works on Windows.
Learn more in our Python docx vs win32com comparison. It explains when to use each approach.
Integrating with Other python-docx Features
Combine charts with other python-docx capabilities. Create complete automated documents.
Use our numbered headings guide for structured reports. Apply Jinja2 templates for dynamic content.
Conclusion
Python-docx cannot add charts directly. But the workarounds are effective and reliable.
Generate charts as images using matplotlib or Plotly. Insert them into your Word documents. The results look professional.
Choose the method that fits your needs. Simple image insertion works for most cases. Template approaches suit standardized reports.
Start adding charts to your automated documents today. Your reports will become much more valuable and informative.