worksheet.hyperlink【ハイパーリンク】モジュール

メモ ( 概要 ハイパーリンクの設定・解除 ) Hyperlink【ハイパーリンク】クラス HyperlinkList【ハイパーリンク リスト】クラス

メモ

概要


クラス備考
親:openpyxl.descriptors.serialisable.Serialisable
class Hyperlink(
    ref=None,
    location=None,
    tooltip=None,
    display=None,
    id=None,
    target=None)
ハイパーリンク
ref (str)設定対象セルアドレス
location (str)ドキュメント内リンク
tooltip (str)ヒント (ツールチップ)
display (str)表示文字列
idId
target (str)リンク先
※ 通常、自動生成
プロパティ備考
displaystr 表示文字列 (内部使用)
※ 表示文字列は、 Cell【セル】value【値】プロパティ 使用
idRelationId (内部使用)
locationstr ドキュメント内リンク
(例)
Excel:
      シート名!セルアドレス
      (シート名が空白を含む場合は、シングルクォートで囲む)
      名前定義
      (workbook.defined_name【名前定義】モジュール 参照)
Word:ブックマーク
Web ページ:タグのId
refstr 設定対象セルアドレス
ハイパーリンクの設定・解除 では自動設定
tagnamestrタグ名 ('hyperlink')
targetstr リンク先ドキュメント (相対パス・絶対パス 可)
ハイパーリンクの設定・解除 も参照
※ 末尾に '#' と location【ドキュメント内リンク】の指定可 (但し、絶対パス指定に不備あり)
(例)
なし:現在のドキュメント
Excel:Excel ファイル名
Word:Word ファイル名
Web ページ:Web ページ URL
メーラー起動:mailto:メールアドレス?subject=件名
その他:フォルダ名・テキストファイル名 等
tooltipstrヒント (ツールチップ)
(改行:'\n')

クラス備考
親:openpyxl.descriptors.serialisable.Serialisable
class HyperlinkList(
    hyperlink=() )
ハイパーリンク リスト
hyperlinkHyperlink のシーケンス
操作備考
bool(HyperlinkList)
ブール演算
len(HyperlinkList)
プロパティ備考
hyperlinklist
tuple
Hyperlink のシーケンス
tagnamestrタグ名 ('hyperlinks')
メソッド備考
append(
    value)

value

from openpyxl import Workbook
from openpyxl.styles import Font, Color

# ハイパーリンク設定
def set_hyperlink(
        cell,           # Cell【セル】
        target='',      # リンク先ドキュメント
        location=None,  # ドキュメント内リンク
        text=None,      # 表示文字列
        tooltip=None,   # ヒント (ツールチップ)
        underline=True, # 下線等指定有無
):
    # 表示文字列
    if text is not None:
        cell.value = text
    else:
        value = target
        if location is not None:
            value += '#' + location
        cell.value = value
    # リンク先ドキュメント
    cell.hyperlink = target
    # ドキュメント内リンク
    if location is not None:
        cell.hyperlink.location = location
    # ヒント (ツールチップ)
    if tooltip is not None:
        cell.hyperlink.tooltip = tooltip
    # 下線等指定
    if underline:
        font = Font(
            color=Color(theme=10),  # Hyperlink
            underline=Font.UNDERLINE_SINGLE,
            # name='~',
            # size=n,
        )
        cell.font = font


# ワークブック作成
WORKBOOK = 'sample.xlsx'
wb = Workbook()
ws = wb.active
ws.title = 'SheetX'
ws.column_dimensions['B'].width = 30
wb.create_sheet('Sheet SP')

# 単一セルへ (同一ブック・同一シート)
ws['B2'] = '単一セルへ'
cell = ws['B3']
cell.value = 'D3 へ (表示文字列)'
print(cell.hyperlink)  # 指定前
# 出力:None
cell.hyperlink = ''    # リンク先ドキュメント (同一ブック)
cell.hyperlink.location = 'SheetX!D3'  # ドキュメント内リンク
print(cell.hyperlink)  # 指定後 (自動生成)
# 出力:
# <openpyxl.worksheet.hyperlink.Hyperlink object>
# Parameters:
# ref='B3', location='SheetX!D3', tooltip=None, display=None, id=None

# 各種 指定
set_hyperlink(
    ws['B4'],
    '',
    'SheetX!D4',
    'D4 へ',
    'TOOLTIP:B4',
)

# 複数セルへ (同一ブック・同一シート)
ws['B6'] = '複数セルへ'
set_hyperlink(
    ws['B7'],
    '',
    'SheetX!D3:F5',
    'D3:F5へ',
    'TOOLTIP:B7',
)

# 名前定義
NAME_TARGET_1 = 'target_1'
NAME_TARGET_2 = 'target_2'
wb.create_named_range(
    NAME_TARGET_1,
    value='SheetX!$D$3, SheetX!$E$4, SheetX!$F$5:$G$6',
)
wb.create_named_range(
    NAME_TARGET_2,
    value="'Sheet SP'!$D$3, 'Sheet SP'!$E$4",
)
# 名前定義セルへ (同一ブック)
ws['B9'] = '名前定義セルへ'
set_hyperlink(
    ws['B10'],
    '',
    f'{NAME_TARGET_1}',
    f'{NAME_TARGET_1} へ',
    'TOOLTIP:B10',
)
set_hyperlink(
    ws['B11'],
    '',
    f'{NAME_TARGET_2}',
    f'{NAME_TARGET_2} へ (別シート)',
    'TOOLTIP:B11',
)

# 別シートへ (同一ブック)
ws['B13'] = '別シートへ'
set_hyperlink(
    ws['B14'],
    '',
    "'Sheet SP'!D3:F5",
    '[Sheet SP] の D3:F5へ',
    'TOOLTIP:B14',
)

# 別ブックへ
ws['B16'] = '別ブックへ'
set_hyperlink(
    ws['B17'],
    'sample-2.xlsx',
    'Sheet1!B2',
    '[sample-2.xlsx]-[Sheet1] の B2へ',
    'TOOLTIP:B17',
)
set_hyperlink(
    ws['B18'],
    'sample SP.xlsx',
    'Sheet1!B2',
    '[sample SP.xlsx]-[Sheet1] の B2へ',
    'TOOLTIP:B18',
)

# 各種ファイル
ws['B20'] = '各種ファイルへ'
set_hyperlink(
    ws['B21'],
    'sample.docx',
    tooltip='TOOLTIP:B21',
)
set_hyperlink(
    ws['B22'],
    'sample.docx',
    'BookMark_1',
    tooltip='TOOLTIP:B22',
)
set_hyperlink(
    ws['B23'],
    'sample.txt',
    tooltip='TOOLTIP:B23',
)

# Web ページ
ws['B25'] = 'Web ページへ'
set_hyperlink(
    ws['B26'],
    'https://cercopes-z.com/',
    None,
    'リファレンス メモ へ',
    'TOOLTIP:B26',
)
set_hyperlink(
    ws['B27'],
    'https://cercopes-z.com/Python/openpyxl/index-opxl.html',
    'basic-class',
    'Web ページ 指定id へ',
    'TOOLTIP:B27',
)
set_hyperlink(  # 解除用
    ws['B28'],
    'https://cercopes-z.com/',
    text='リファレンス メモ へ',
)

# ハイパーリンクの解除
ws['B28'].value += ' (解除)'
ws['B28'].hyperlink = None
ws['B28'].font = None

# メーラー起動
ws['B30'] = 'メーラー起動'
set_hyperlink(
    ws['B31'],
    'mailto:メールアドレス?subject=件名',
    tooltip='TOOLTIP:B31',
)

# ワークブック保存
wb.save(WORKBOOK)

ハイパーリンクのサンプル画像