Skip to content

Conversation

KehaoWu
Copy link

@KehaoWu KehaoWu commented Apr 27, 2017

Hi, I create a new feature branch to add new feature: add_chart for docx.

The add_chart method allow us to add chart from python-pptx to docx. I request to merge to master branch and release as v0.8.7 or something.

Best wishes.

@mrxliu
Copy link

mrxliu commented May 20, 2017

good job. but merge failed

@sdnu
Copy link

sdnu commented Jun 5, 2017

good job. this use to my project

@s1murg
Copy link

s1murg commented Dec 1, 2018

Can you put an example for use

@vba34520
Copy link

vba34520 commented Nov 17, 2020

It does work, Thanks!

My version

python-docx 0.8.10
python-pptx 0.6.18

Attention, in my version, docx/parts/document.py should import another class.

from pptx.parts.chart import ChartPart
from docx.oxml.shape import CT_Inline

chart can be editted.

Example code

from docx import Document
from pptx.util import Pt, Inches
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE, XL_LEGEND_POSITION, XL_DATA_LABEL_POSITION

document = Document()
chart_data = CategoryChartData()
chart_data.categories = ['East', 'West', 'Midwest']
chart_data.add_series('Series 1', (19.2, 21.4, 16.7))
x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
chart = document.add_chart(XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data)

chart.has_legend = True
chart.legend.position = XL_LEGEND_POSITION.BOTTOM
chart.legend.include_in_layout = False

plot = chart.plots[0]
plot.has_data_labels = True
data_labels = plot.data_labels
data_labels.font.size = Pt(13)
data_labels.position = XL_DATA_LABEL_POSITION.OUTSIDE_END

chart.has_title = True
chart_title = chart.chart_title
text_frame = chart_title.text_frame
text_frame.text = 'Title'
paragraphs = text_frame.paragraphs
paragraph = paragraphs[0]
paragraph.font.size = Pt(18)

category_axis = chart.category_axis
category_axis.tick_labels.font.size = Pt(14)

document.save('test.docx')

Result
image

You can directly download the editted files. Use the folder of 替换 to cover the files on folder of docx. Like C:\Users\Administrator\AppData\Local\Programs\Python\Python36\Lib\site-packages\docx

@lfjbb
Copy link

lfjbb commented Nov 30, 2020

why chart add in run,in my opinion chart is a part. actually, what I need is replace the data in chart. I can create template myself, but I need to render the xml, and replace the chart data, autofit the axis. I think this is what most people need.

@ym500
Copy link

ym500 commented Dec 29, 2021

can this method be used with python docx template?

@nszceta
Copy link

nszceta commented Apr 1, 2022

Is there any update on this issue?

@sachinwhiteman7
Copy link

@vba34520
I tried the example you mentioned and it works.
But how can i remove the border around the chart.

@super-tian
Copy link

why chart add in run,in my opinion chart is a part. actually, what I need is replace the data in chart. I can create template myself, but I need to render the xml, and replace the chart data, autofit the axis. I think this is what most people need.

i have try to use minidom to modify the base chart.xml ,it useful,maybe you can try it too.
when i finish my python script ,i will upload to github

@workamiracle
Copy link

While implementing @vba34520 's code snippet, I got this error: AttributeError: 'Document' object has no attribute 'add_chart'.
How can I solve it?

@benkyousec
Copy link

benkyousec commented Aug 21, 2023

While implementing @vba34520 's code snippet, I got this error: AttributeError: 'Document' object has no attribute 'add_chart'. How can I solve it?

@workamiracle You need to be using their version of python-docx because add_chart is not part of the main repo. I also suggest using an older version of python because some of the modules used have been moved in 3.10. (Working on python 3.6)

@JiangyouTester
Copy link

Any update on this issue?

@mengdeer589
Copy link

mengdeer589 commented Jun 3, 2024

请在此处获取monkey.py文件monkey.txt
我通过整合KehaoWu的代码,创建了monkey.py这个文件,使用monkey patching的办法为docx增加了添加可编辑图表的功能。 使用示例参考
1,需要在代码中引入该文件import monkey,,
2,需修改docx/oxml/shape.py,65行,增加cChart = ZeroOrOne('c:chart'),即可成功使用。
测试版本:
python 3.11
python-docx 1.1.2
python-pptx 0.6.23

示例代码:
`from docx import Document
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Cm

import monkey

document = Document()
chart_data = CategoryChartData()
chart_data.categories = ["apple", "banana", "grape"]
chart_data.add_series("Series 1", (19, 30, 7))
chart = document.add_chart(
XL_CHART_TYPE.COLUMN_CLUSTERED, 0, 0, Cm(15.2), Cm(11.4), chart_data
)

document.save("test.docx")
`
不过未经严格测试,请谨慎使用

@SumitRathor002
Copy link

@vba34520 I tried the example you mentioned and it works. But how can i remove the border around the chart.

Did you find any way to remove the borders or "outlines" around the chart?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.