{"slug": "creating-and-customizing-charts-in-word-documents-using-python", "title": "Creating and Customizing Charts in Word Documents Using Python", "summary": "A developer demonstrates how to create and customize charts in Word documents using Python with the Spire.Doc library. The article provides code examples for column, line, and pie charts, including setting titles, data series, and axis formats, enabling automated report generation.", "body_md": "Charts are essential tools for data visualization, transforming abstract numbers into intuitive graphical representations. Embedding charts in Word documents makes reports, proposals, and analysis documents more compelling. The traditional approach of manually inserting charts in Word and editing data one by one is inefficient and prone to inconsistency. By creating charts programmatically with Python, you can build an automated pipeline from data to documents, generating standardized reports with charts in batch. This article introduces how to create multiple chart types in Word documents using Python, including setting chart titles, data series, and axis formats.\n\nCompared to manual operation, the programmatic approach offers the following advantages:\n\nThis article uses Spire.Doc for Python, which provides APIs for inserting and customizing charts in Word documents.\n\n```\npip install Spire.Doc\n```\n\nAfter installation, you can import the relevant modules in your Python script and start working.\n\nColumn charts are one of the most commonly used chart types, suitable for comparing values across categories. The following code demonstrates how to create a column chart with a data series:\n\n```\nfrom spire.doc import *\n\noutputFile = \"ColumnChart.docx\"\n\n# Create a document and add a section\ndocument = Document()\nsection = document.AddSection()\n\n# Add descriptive text\nsection.AddParagraph().AppendText(\"Column chart.\")\n\n# Add a paragraph to host the chart\nnewPara = section.AddParagraph()\n\n# Append a column chart with specified width and height\nshape = newPara.AppendChart(ChartType.Column, float(500), float(300))\nchart = shape.Chart\n\n# Clear default data series\nchart.Series.Clear()\n\n# Add a custom data series\nchart.Series.Add(\"Test Series\",\n                 [\"Word\", \"PDF\", \"Excel\", \"GoogleDocs\", \"Office\"], [\n                     float(1900000),\n                     float(850000),\n                     float(2100000),\n                     float(600000),\n                     float(1500000)\n                 ])\n\n# Set Y-axis number format\nchart.AxisY.NumberFormat.FormatCode = \"#,##0\"\n\n# Save the document\ndocument.SaveToFile(outputFile, FileFormat.Docx)\ndocument.Dispose()\n```\n\nThe generated Word document:\n\nKey steps in the code:\n\n`AppendChart(ChartType.Column, width, height)`\n\nmethod adds a chart shape to the paragraph and returns a chart shape object.`Series.Add()`\n\nmethod accepts three parameters — series name, category list (X-axis), and value list (Y-axis).`AxisY.NumberFormat.FormatCode`\n\nsets the display format for Y-axis labels. `\"#,##0\"`\n\napplies thousand separators.Line charts are ideal for showing trends over time or categories. The following code creates a line chart with multiple data series and a chart title:\n\n```\nfrom spire.doc import *\n\noutputFile = \"LineChart.docx\"\n\ndocument = Document()\nsection = document.AddSection()\nsection.AddParagraph().AppendText(\"Line chart.\")\n\nnewPara = section.AddParagraph()\n\n# Append a line chart\nshape = newPara.AppendChart(ChartType.Line, 500.0, 300.0)\nchart = shape.Chart\n\n# Set chart title\ntitle = chart.Title\ntitle.Text = \"My Chart\"\ntitle.Show = True\ntitle.Overlay = True\n\n# Add multiple data series\nseriesColl = chart.Series\nseriesColl.Clear()\n\ncategories = [\"C1\", \"C2\", \"C3\", \"C4\", \"C5\", \"C6\"]\n\nseriesColl.Add(\"AW Series 1\", categories, [1.0, 2.0, 2.5, 4.0, 5.0, 6.0])\nseriesColl.Add(\"AW Series 2\", categories, [2.0, 3.0, 3.5, 6.0, 6.5, 7.0])\n\ndocument.SaveToFile(outputFile, FileFormat.Docx)\ndocument.Dispose()\n```\n\nThe generated Word document:\n\nTitle settings explained:\n\n`title.Text`\n\n: Sets the title text content`title.Show`\n\n: Controls whether the title is displayed`title.Overlay`\n\n: When set to `True`\n\n, the title overlays the chart without taking up additional spacePie charts display the proportional relationship of parts to a whole. A pie chart requires only one data series:\n\n```\nfrom spire.doc import *\n\noutputFile = \"PieChart.docx\"\n\ndocument = Document()\nsection = document.AddSection()\nsection.AddParagraph().AppendText(\"Pie chart.\")\n\nnewPara = section.AddParagraph()\n\n# Append a pie chart\nshape = newPara.AppendChart(ChartType.Pie, 500.0, 300.0)\nchart = shape.Chart\n\n# Add a data series\nchart.Series.Add(\"Test Series\", [\"Word\", \"PDF\", \"Excel\"],\n                          [2.7, 3.2, 0.8])\n\ndocument.SaveToFile(outputFile, FileFormat.Docx)\ndocument.Dispose()\n```\n\nThe data series addition works the same way as with column charts. `ChartType.Pie`\n\nspecifies the chart type as a pie chart.\n\nScatter charts show the correlation between two sets of data, while bubble charts add a third dimension to represent data weight.\n\n**Scatter chart**:\n\n```\nfrom spire.doc import *\n\noutputFile = \"ScatterChart.docx\"\n\ndocument = Document()\nsection = document.AddSection()\nsection.AddParagraph().AppendText(\"Scatter chart.\")\n\nnewPara = section.AddParagraph()\n\n# Append a scatter chart\nshape = newPara.AppendChart(ChartType.Scatter, 450.0, 300.0)\nchart = shape.Chart\nchart.Series.Clear()\n\n# Add a data series with X and Y value lists\nchart.Series.Add(\"Scatter chart\", [1.0, 2.0, 3.0, 4.0, 5.0],\n                 [1.0, 20.0, 40.0, 80.0, 160.0])\n\ndocument.SaveToFile(outputFile, FileFormat.Docx)\ndocument.Dispose()\n```\n\n**Bubble chart**:\n\n```\nfrom spire.doc import *\n\noutputFile = \"BubbleChart.docx\"\n\ndocument = Document()\nsection = document.AddSection()\nsection.AddParagraph().AppendText(\"Bubble chart.\")\n\nnewPara = section.AddParagraph()\n\n# Append a bubble chart\nshape = newPara.AppendChart(ChartType.Bubble, float(500), float(300))\nchart = shape.Chart\nchart.Series.Clear()\n\n# Add a data series with X, Y, and bubble size lists\nchart.Series.Add(\"Test Series\",\n                 [2.9, 3.5, 1.1, 4.0, 4.0],\n                 [1.9, 8.5, 2.1, 6.0, 1.5],\n                 [9.0, 4.5, 2.5, 8.0, 5.0])\n\ndocument.SaveToFile(outputFile, FileFormat.Docx)\ndocument.Dispose()\n```\n\nThe key difference lies in the number of parameters passed to `Series.Add()`\n\n: scatter charts take X and Y lists, while bubble charts take an additional third list for bubble sizes.\n\n3D surface charts are suitable for displaying the three-dimensional distribution of multivariate data. The following code creates a surface chart with three data series:\n\n```\nfrom spire.doc import *\n\noutputFile = \"Surface3DChart.docx\"\n\ndocument = Document()\nsection = document.AddSection()\nsection.AddParagraph().AppendText(\"Surface3D chart.\")\n\nnewPara = section.AddParagraph()\n\n# Append a 3D surface chart\nshape = newPara.AppendChart(ChartType.Surface3D, 500.0, 300.0)\nchart = shape.Chart\nchart.Series.Clear()\n\n# Set title\nchart.Title.Text = \"My chart\"\n\n# Add multiple data series\ncategories = [\"Word\", \"PDF\", \"Excel\", \"GoogleDocs\", \"Office\"]\n\nchart.Series.Add(\"Series 1\", categories,\n                 [1900000.0, 850000.0, 2100000.0, 600000.0, 1500000.0])\nchart.Series.Add(\"Series 2\", categories,\n                 [900000.0, 50000.0, 1100000.0, 400000.0, 250000.0])\nchart.Series.Add(\"Series 3\", categories,\n                 [500000.0, 820000.0, 1500000.0, 400000.0, 100000.0])\n\ndocument.SaveToFile(outputFile, FileFormat.Docx)\ndocument.Dispose()\n```\n\nBy adding multiple data series, the surface chart can simultaneously display the 3D distribution of multiple data groups, making it suitable for comparative analysis.\n\nIn practice, you can encapsulate chart creation in a function to quickly add multiple charts to a single document:\n\n``` python\ndef create_chart(document, chart_type, title, categories, values):\n    section = document.AddSection()\n    section.AddParagraph().AppendText(title)\n    para = section.AddParagraph()\n    shape = para.AppendChart(chart_type, 500.0, 300.0)\n    chart = shape.Chart\n    chart.Series.Clear()\n    chart.Title.Text = title\n    chart.Title.Show = True\n    chart.Series.Add(\"Data\", categories, values)\n    return chart\n\n# Usage example\ndoc = Document()\ncreate_chart(doc, ChartType.Column, \"Quarterly Sales\",\n             [\"Q1\", \"Q2\", \"Q3\", \"Q4\"], [120.0, 150.0, 180.0, 200.0])\ncreate_chart(doc, ChartType.Line, \"Monthly Growth Trend\",\n             [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\"],\n             [10.0, 15.0, 22.0, 28.0, 35.0, 42.0])\ndoc.SaveToFile(\"Report.docx\", FileFormat.Docx)\ndoc.Dispose()\n```\n\nAfter encapsulating chart creation as a function, you can quickly add various chart types to the same document, which is useful for generating comprehensive reports with multiple data views.\n\nFor large numerical data, setting axis number formats improves readability:\n\n```\n# Thousand separator format\nchart.AxisY.NumberFormat.FormatCode = \"#,##0\"\n\n# Percentage format\nchart.AxisY.NumberFormat.FormatCode = \"0%\"\n\n# Thousand separator with decimal places\nchart.AxisY.NumberFormat.FormatCode = \"#,##0.00\"\n```\n\nThis article covered the complete workflow for creating charts in Word documents using Python, including six chart types: column, line, pie, scatter, bubble, and 3D surface charts.\n\nKey takeaways:\n\n`AppendChart(ChartType, width, height)`\n\nto append a chart to a paragraph, specifying the chart type via the `ChartType`\n\nenum`chart.Series.Add()`\n\n— the number of parameters varies by chart type (scatter charts need X/Y lists, bubble charts need X/Y/size lists)`chart.Title`\n\n`chart.AxisY.NumberFormat.FormatCode`\n\nWith these skills, you can integrate chart creation into report generation pipelines, automatically producing Word documents with visualized charts from business data, significantly improving the efficiency and consistency of data report generation.", "url": "https://wpnews.pro/news/creating-and-customizing-charts-in-word-documents-using-python", "canonical_source": "https://dev.to/allen_yang_f905170c5a197b/creating-and-customizing-charts-in-word-documents-using-python-1j6g", "published_at": "2026-09-03 09:32:43+00:00", "updated_at": "2026-09-03 09:54:00.528238+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["Spire.Doc"], "alternates": {"html": "https://wpnews.pro/news/creating-and-customizing-charts-in-word-documents-using-python", "markdown": "https://wpnews.pro/news/creating-and-customizing-charts-in-word-documents-using-python.md", "text": "https://wpnews.pro/news/creating-and-customizing-charts-in-word-documents-using-python.txt", "jsonld": "https://wpnews.pro/news/creating-and-customizing-charts-in-word-documents-using-python.jsonld"}}