Pythonでプレゼンテーションのハイパーリンクを管理する
概要
ハイパーリンクは外部リソース、オブジェクトまたはデータ項目、あるいはファイル内の特定の場所への参照です。PowerPoint プレゼンテーションで一般的なハイパーリンクの種類は次のとおりです。
- テキスト、図形、またはメディアに埋め込まれた Web サイトへのリンク
- スライドへのリンク
Aspose.Slides for Python via .NET は、プレゼンテーション内でハイパーリンクに関するさまざまな操作を可能にします。
URL ハイパーリンクの追加
このセクションでは、Aspose.Slides を使用してスライド要素に URL ハイパーリンクを追加する方法を説明します。テキスト、図形、画像にリンク先アドレスを割り当て、プレゼンテーション中のスムーズなナビゲーションを実現します。
テキストへの URL ハイパーリンクの追加
次のコード例は、テキストに Web サイトへのハイパーリンクを追加する方法を示しています:
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 100, 100, 600, 50, False)
shape.add_text_frame("Aspose: File Format APIs")
text_portion = shape.text_frame.paragraphs[0].portions[0]
text_portion.portion_format.hyperlink_click = slides.Hyperlink("https://www.aspose.com/")
text_portion.portion_format.hyperlink_click.tooltip = "More than 70% of Fortune 100 companies trust Aspose APIs."
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
図形またはフレームへの URL ハイパーリンクの追加
次のコード例は、図形に Web サイトへのハイパーリンクを追加する方法を示しています:
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 100, 100, 600, 50)
shape.hyperlink_click = slides.Hyperlink("https://www.aspose.com/")
shape.hyperlink_click.tooltip = "More than 70% of Fortune 100 companies trust Aspose APIs."
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
メディアへの URL ハイパーリンクの追加
Aspose.Slides では、画像、音声、動画ファイルにハイパーリンクを追加できます。
次のコード例は、画像 にハイパーリンクを追加する方法を示しています:
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
# プレゼンテーションに画像を追加します。
with open("image.jpeg", "rb") as image_stream:
image_data = image_stream.read()
image = presentation.images.add_image(image_data)
# 先ほど追加した画像を使用してスライド1に画像フレームを作成します。
picture_frame = slide.shapes.add_picture_frame(slides.ShapeType.RECTANGLE, 10, 10, 100, 100, image)
picture_frame.hyperlink_click = slides.Hyperlink("https://www.aspose.com/")
picture_frame.hyperlink_click.tooltip = "More than 70% of Fortune 100 companies trust Aspose APIs."
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
次のコード例は、音声ファイル にハイパーリンクを追加する方法を示しています:
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
with open("audio.mp3", "rb") as audio_stream:
audio_data = audio_stream.read()
audio = presentation.audios.add_audio(audio_data)
audio_frame = slide.shapes.add_audio_frame_embedded(10, 10, 100, 100, audio)
audio_frame.hyperlink_click = slides.Hyperlink("https://www.aspose.com/")
audio_frame.hyperlink_click.tooltip = "More than 70% of Fortune 100 companies trust Aspose APIs."
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
次のコード例は、動画 にハイパーリンクを追加する方法を示しています:
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
with open("video.avi", "rb") as video_stream:
video_data = video_stream.read()
video = presentation.videos.add_video(video_data)
video_frame = slide.shapes.add_video_frame(10, 10, 100, 100, video)
video_frame.hyperlink_click = slides.Hyperlink("https://www.aspose.com/")
video_frame.hyperlink_click.tooltip = "More than 70% of Fortune 100 companies trust Aspose APIs."
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
Tip
Python を使用したプレゼンテーションでの OLE の管理については、Python を使用したプレゼンテーションでの OLE の管理をご覧ください。目次の作成にハイパーリンクを使用する
ハイパーリンクはオブジェクトや場所を参照できるため、目次を作成する際にも利用できます。
以下のサンプルコードは、ハイパーリンク付きの目次を作成する方法を示しています:
import aspose.slides as slides
with slides.Presentation() as presentation:
first_slide = presentation.slides[0]
second_slide = presentation.slides.add_empty_slide(first_slide.layout_slide)
content_table = first_slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 40, 40, 300, 100)
content_table.fill_format.fill_type = slides.FillType.NO_FILL
content_table.line_format.fill_format.fill_type = slides.FillType.NO_FILL
content_table.text_frame.paragraphs.clear()
paragraph = slides.Paragraph()
paragraph.paragraph_format.default_portion_format.fill_format.fill_type = slides.FillType.SOLID
paragraph.paragraph_format.default_portion_format.fill_format.solid_fill_color.color = draw.Color.black
paragraph.text = "Title of slide 2 .......... "
link_text_portion = slides.Portion()
link_text_portion.text = "Page 2"
link_text_portion.portion_format.hyperlink_manager.set_internal_hyperlink_click(second_slide)
paragraph.portions.add(link_text_portion)
content_table.text_frame.paragraphs.add(paragraph)
presentation.save("link_to_slide.pptx", slides.export.SaveFormat.PPTX)
ハイパーリンクの書式設定
このセクションでは、Aspose.Slides でハイパーリンクの外観を設定する方法を示します。テキスト、図形、画像に対して色やその他のスタイルオプションを制御し、ハイパーリンクの書式を一貫させる方法を学びます。
ハイパーリンクの色
Hyperlink クラスの color_source プロパティを使用すると、ハイパーリンクの色を設定したり、色情報を取得したりできます。この機能は PowerPoint 2019 で導入されたため、以前のバージョンの PowerPoint には適用されません。
次のサンプルは、同じスライド上に異なる色のハイパーリンクを追加する方法を示しています:
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
shape1 = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 100, 100, 600, 50, False)
shape1.add_text_frame("This is a sample of a colored hyperlink.")
text_portion1 = shape1.text_frame.paragraphs[0].portions[0]
text_portion1.portion_format.hyperlink_click = slides.Hyperlink("https://www.aspose.com/")
text_portion1.portion_format.hyperlink_click.color_source = slides.HyperlinkColorSource.PORTION_FORMAT
text_portion1.portion_format.fill_format.fill_type = slides.FillType.SOLID
text_portion1.portion_format.fill_format.solid_fill_color.color = draw.Color.red
shape2 = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 100, 200, 450, 50, False)
shape2.add_text_frame("This is a sample of a regular hyperlink.")
text_portion2 = shape2.text_frame.paragraphs[0].portions[0]
text_portion2.portion_format.hyperlink_click = slides.Hyperlink("https://www.aspose.com/")
presentation.save("hyperlinks.pptx", slides.export.SaveFormat.PPTX)
プレゼンテーションからハイパーリンクを削除する
このセクションでは、Aspose.Slides を使用してプレゼンテーションからハイパーリンクを削除する方法を説明します。テキスト、図形、画像からリンク先をクリアし、元のコンテンツと書式を保持したままハイパーリンクを除去します。
テキストからハイパーリンクを削除する
次のサンプルコードは、スライド上のテキストからハイパーリンクを削除する方法を示しています:
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
slide = presentation.slides[0]
for shape in slide.shapes:
if type(shape) is slides.AutoShape:
for paragraph in shape.text_frame.paragraphs:
for text_portion in paragraph.portions:
text_portion.portion_format.hyperlink_manager.remove_hyperlink_click()
presentation.save("removed_hyperlinks.pptx", slides.export.SaveFormat.PPTX)
図形またはフレームからハイパーリンクを削除する
次のサンプルコードは、スライド上の図形からハイパーリンクを削除する方法を示しています:
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
slide = presentation.slides[0]
for shape in slide.shapes:
shape.hyperlink_manager.remove_hyperlink_click()
presentation.save("removed_hyperlinks.pptx", slides.export.SaveFormat.PPTX)
可変ハイパーリンク
Hyperlink クラスは可変です。このクラスを使用すると、次のプロパティの値を変更できます。
次のコードスニペットは、スライドにハイパーリンクを追加し、そのツールチップを編集する方法を示しています:
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 100, 100, 600, 50, False)
shape.add_text_frame("Aspose: File Format APIs")
text_portion = shape.text_frame.paragraphs[0].portions[0]
text_portion.portion_format.hyperlink_click = slides.Hyperlink("https://www.aspose.com/")
text_portion.portion_format.hyperlink_click.tooltip = "More than 70% of Fortune 100 companies trust Aspose APIs."
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
IHyperlinkQueries でサポートされているプロパティ
プレゼンテーション、スライド、またはハイパーリンクを含むテキストから HyperlinkQueries にアクセスできます。
HyperlinkQueries クラスは次のメソッドをサポートします:
FAQ
スライドだけでなく「セクション」やセクションの最初のスライドへ内部ナビゲーションを作成するにはどうすればよいですか?
PowerPoint のセクションはスライドのグループです。ナビゲーションは技術的に特定のスライドを対象とするため、セクションに「移動」する場合は通常、そのセクションの最初のスライドへリンクします。
マスタースライドの要素にハイパーリンクを付けて、すべてのスライドで機能させることはできますか?
はい。マスタースライドおよびレイアウト要素はハイパーリンクをサポートしています。このリンクは子スライドに引き継がれ、スライドショー中にクリック可能です。
PDF、HTML、画像、またはビデオにエクスポートした場合、ハイパーリンクは保持されますか?
PDF と HTML では、リンクは概ね保持されます。画像 と ビデオ にエクスポートした場合は、ラスタフレームやビデオがハイパーリンクをサポートしないため、クリック可能性は引き継がれません。