🐍

Excel内のファイルパスリンクの疎通を確認するpythonコード

2025/02/11に公開

概要

Excel内のファイルパスのリンクが知らないうちに切れることはありませんか?
例えばリンク先のファイルが勝手に移動していた時にこういったことがよくあります。
なので同じをフォルダ内のすべてのExcelファイルを調べ、ファイルパスが切れているリンクのあるセルを選択できるコードを作りました。
win32comを使用しているのでwindowsでしか動かないと思います。

コード全文

# import os
# import win32com.client
# from openpyxl import load_workbook
# import tkinter as tk
# from tkinter import messagebox, Listbox, Scrollbar

# def check_links_in_excel(file_path):
#     wb = load_workbook(file_path, data_only=True)
#     links = []

#     for sheet in wb.sheetnames:
#         ws = wb[sheet]
#         for row in ws.iter_rows():
#             for cell in row:
#                 if cell.hyperlink:
#                     # セルのアドレスとリンクを保存
#                     links.append((os.path.basename(file_path), sheet, cell.coordinate, cell.hyperlink.target))

#     return links

# def check_file_exists(file_path):
#     # 絶対パスに変換してファイルの存在をチェック
#     absolute_path = os.path.abspath(file_path)
#     return os.path.isfile(absolute_path)

# def open_excel_and_select_cell(excel_file_path, sheet_name, cell_address):
#     excel = win32com.client.Dispatch("Excel.Application")
#     excel.Visible = True  # Excelを表示する
#     wb = excel.Workbooks.Open(os.path.abspath(excel_file_path))
#     ws = wb.Sheets(sheet_name)
#     ws.Select()
#     ws.Range(cell_address).Select()

# def show_link_errors(links):
#     # Tkinterウィンドウを作成
#     window = tk.Tk()
#     window.title("リンク切れのリスト")

#     # スクロールバーを作成
#     scrollbar = Scrollbar(window)
#     scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

#     # リストボックスを作成
#     listbox = Listbox(window, yscrollcommand=scrollbar.set, width=80, height=20)
#     for file_name, sheet, cell_address, link in links:
#         listbox.insert(tk.END, f"{file_name} の {sheet} の {cell_address} - {link}")
#     listbox.pack()

#     scrollbar.config(command=listbox.yview)

#     def on_select():
#         selected = listbox.curselection()
#         if selected:
#             index = selected[0]
#             selected_item = links[index]  # タプルを取得
#             # 選択されたリンクに基づいてファイル名を取得
#             selected_file_name = selected_item[0]
#             selected_sheet_name = selected_item[1]
#             selected_cell_address = selected_item[2]
#             selected_file_path = os.path.join(current_directory, selected_file_name)  # 現在のディレクトリからファイルパスを作成
#             open_excel_and_select_cell(selected_file_path, selected_sheet_name, selected_cell_address)

#     # 選択ボタンを作成
#     select_button = tk.Button(window, text="選択", command=on_select)
#     select_button.pack()

#     # ウィンドウを表示
#     window.mainloop()

# def check_all_excel_files_in_directory(directory):
#     all_links = []
#     for filename in os.listdir(directory):
#         if filename.endswith(('.xlsx', '.xlsm')):
#             file_path = os.path.join(directory, filename)
#             links = check_links_in_excel(file_path)
#             all_links.extend(links)

#     return all_links

# if __name__ == "__main__":
#     # 現在の作業ディレクトリを取得
#     current_directory = os.getcwd()
#     links = check_all_excel_files_in_directory(current_directory)

#     link_errors = []
#     for file_name, sheet, cell_address, link in links:
#         file_exists = check_file_exists(link)
#         if file_exists:
#             print(f"ファイル: {link} - 存在します。")
#         else:
#             link_errors.append((file_name, sheet, cell_address, link))
#             print(f"ファイル: {link} - 存在しません。セル: {sheet}の{cell_address}")

#     if link_errors:
#         show_link_errors(link_errors)
#     else:
#         messagebox.showinfo("情報", "すべてのリンクは正常です。")

フォルダ内のE各Excelファイルのシートのリンクの疎通を確認してリンク切れがあればGUI(Tkinter)で一覧を表示。
選択したリンクのセルをExcelで開くという単純なものです。
試してみてください。

Discussion