Created
August 31, 2023 20:27
-
-
Save rdapaz/da0c87aecd90240ad5360d02a307da61 to your computer and use it in GitHub Desktop.
Create Tables in Powerpoint with hyperlinks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import win32com.client | |
# Open the PowerPoint application and the presentation | |
Application = win32com.client.Dispatch("PowerPoint.Application") | |
pptPath = r'path\to\powerpoint.pptx' | |
Presentation = Application.Presentations.Open(pptPath) | |
# Dictionaries to store the results | |
reverse_index_hash = {} | |
forward_index_hash = {} | |
for slide_num, slide in enumerate(Presentation.Slides, 1): | |
for shape in slide.Shapes: | |
if shape.HasTable: | |
table = shape.Table | |
if table.Cell(1, 1).Shape.TextFrame.TextRange.Text == "System": | |
# Check the condition for the first dictionary | |
if table.Cell(2, 1).Shape.TextFrame.TextRange.Text != "Worst-Case Risks": | |
# Starting from the third row, collect content of cells in column 1 | |
for row in range(2, table.Rows.Count + 1): | |
content = table.Cell(row, 1).Shape.TextFrame.TextRange.Text | |
if content: # check if not empty | |
reverse_index_hash[content] = str(Presentation.Slides(slide_num).SlideNumber) + '. ' + \ | |
Presentation.Slides(slide_num).Name | |
elif table.Cell(2, 1).Shape.TextFrame.TextRange.Text == "Worst-Case Risks": | |
val = table.Cell(1, 2).Shape.TextFrame.TextRange.Text | |
if val: | |
forward_index_hash[val] = str(Presentation.Slides(slide_num).SlideNumber) + '. ' + \ | |
Presentation.Slides(slide_num).Name | |
if val in reverse_index_hash: | |
address_reference = reverse_index_hash[val] | |
hyperlink = table.Cell(1, 2).Shape.TextFrame.TextRange.ActionSettings(1).Hyperlink | |
hyperlink.SubAddress = address_reference | |
for slide_num, slide in enumerate(Presentation.Slides, 1): | |
for shape in slide.Shapes: | |
if shape.HasTable: | |
table = shape.Table | |
if table.Cell(1, 1).Shape.TextFrame.TextRange.Text == "System": | |
# Check the condition for the first dictionary | |
if table.Cell(2, 1).Shape.TextFrame.TextRange.Text != "Worst-Case Risks": | |
for row in range(2, table.Rows.Count + 1): | |
content = table.Cell(row, 1).Shape.TextFrame.TextRange.Text | |
if content and content in forward_index_hash: | |
address_reference = forward_index_hash[content]# check if not empty | |
hyperlink = table.Cell(row, 1).Shape.TextFrame.TextRange.ActionSettings(1).Hyperlink | |
hyperlink.SubAddress = address_reference | |
# Presentation.Close() | |
# Application.Quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment