python-pptx icon indicating copy to clipboard operation
python-pptx copied to clipboard

Cant put the hyperlink on the chart

Open Disha819 opened this issue 1 year ago • 1 comments

I am trying to add the hyper link on the chart in the document it says that chart can have hyperlink, but there is no such example. Hyperlinkable shapes These shape types can have hyperlinks:

Autoshapes Textbox Picture Connector Chart

This is what I have tried but its not working. from pptx import Presentation from pptx.chart.data import CategoryChartData from pptx.enum.chart import XL_CHART_TYPE from pptx.util import Inches

Create a presentation object

prs = Presentation()

Add a slide

slide_layout = prs.slide_layouts[5] # Choose the layout (Title and Content) slide = prs.slides.add_slide(slide_layout)

Define chart data

chart_data = CategoryChartData() chart_data.categories = ['Category 1', 'Category 2', 'Category 3'] chart_data.add_series('Series 1', (1, 2, 3))

Add a chart to the slide

x, y, cx, cy = Inches(1), Inches(1), Inches(8), Inches(4.5) # Adjust the dimensions as needed chart_slide = slide.shapes.add_chart( XL_CHART_TYPE.BAR_CLUSTERED, x, y, cx, cy, chart_data ).chart

Add a title to the chart

chart_slide.has_title = True chart_slide.chart_title.text_frame.text = "Chart Title with Hyperlink"

Add a hyperlink to the presentation

hyperlink_address = "https://theinsightsfamily.com/" hyperlink = prs.slides._sldIdLst[0].add_hyperlink_relationship(hyperlink_address)

Apply the hyperlink to the chart title

chart_slide.chart_title.text_frame.paragraphs[0].runs[0].hyperlink = hyperlink

Save the presentation

prs.save("bar_chart_with_hyperlink.pptx")

and this is error is coming Cell In[6], line 36 hyperlink = prs.slides._sldIdLst[0].add_hyperlink_relationship(hyperlink_address)

AttributeError: 'CT_SlideId' object has no attribute 'add_hyperlink_relationship'

Disha819 avatar Mar 27 '24 18:03 Disha819

Try with chart_slide.chart_title.text_frame.paragraphs[0].runs[0].hyperlink._add_hlinkClick(hyperlink) instead of chart_slide.chart_title.text_frame.paragraphs[0].runs[0].hyperlink = hyperlink

Mike3285 avatar Sep 13 '24 09:09 Mike3285