テキストファイル比較ツール『偽典抹消詩篇:断章識別』 ソースコード

以下は『偽典抹消詩篇:断章識別』のソースコード全文です:

 
        import tkinter as tk
        from tkinter import filedialog, messagebox, scrolledtext
        import os
        from datetime import datetime
        
        class FileComparatorApp:
            def __init__(self, master):
                self.master = master
                master.title("テキストファイル比較ツール - 偽典抹消詩篇:断章識別")
        
                # ファイルパス用変数
                self.file1_path = tk.StringVar()
                self.file2_path = tk.StringVar()
        
                # GUI構築
                self.create_widgets()
        
            def create_widgets(self):
                # ファイル1
                tk.Button(self.master, text="ファイル1", command=self.select_file1).grid(row=0, column=0, padx=5, pady=5)
                tk.Entry(self.master, textvariable=self.file1_path, width=80).grid(row=0, column=1, padx=5, pady=5)
        
                # ファイル2
                tk.Button(self.master, text="ファイル2", command=self.select_file2).grid(row=1, column=0, padx=5, pady=5)
                tk.Entry(self.master, textvariable=self.file2_path, width=80).grid(row=1, column=1, padx=5, pady=5)
        
                # 比較ボタン
                tk.Button(self.master, text="比較", command=self.compare_files).grid(row=2, column=0, padx=5, pady=5)
        
                # 書き出しボタン
                tk.Button(self.master, text="書き出し", command=self.export_result).grid(row=2, column=1, padx=5, pady=5, sticky='e')
        
                # ステータスウインドウ(スクロール付きテキスト)
                self.status_text = scrolledtext.ScrolledText(self.master, width=100, height=15)
                self.status_text.grid(row=3, column=0, columnspan=2, padx=5, pady=5)
        
            def select_file1(self):
                path = filedialog.askopenfilename(filetypes=[("All files", "*.*")])
                if path:
                    self.file1_path.set(path)
        
            def select_file2(self):
                path = filedialog.askopenfilename(filetypes=[("All files", "*.*")])
                if path:
                    self.file2_path.set(path)
        
            def compare_files(self):
                file1 = self.file1_path.get()
                file2 = self.file2_path.get()
            
                if not file1 or not file2:
                    messagebox.showwarning("警告", "両方のファイルを選択してください。")
                    return
            
                try:
                    with open(file1, encoding="utf-8-sig") as f1, open(file2, encoding="utf-8-sig") as f2:
                        lines1 = [line.rstrip() for line in f1]
                        lines2 = [line.rstrip() for line in f2]
            
                    result = []
                    max_len = max(len(lines1), len(lines2))
                    for i in range(max_len):
                        line1 = lines1[i] if i < len(lines1) else ""
                        line2 = lines2[i] if i < len(lines2) else ""
                        if line1 != line2:
                            result.append(f"{i+1}行目で不一致")
                            result.append(f"ファイル1:{line1}")
                            result.append(f"ファイル2:{line2}")
                            break  # 最初の不一致で終了
            
                    if not result:
                        result.append("全ての行が一致しています。")
            
                    self.status_text.delete(1.0, tk.END)
                    self.status_text.insert(tk.END, "\n".join(result))
            
                    messagebox.showinfo("完了", "比較が完了しました。")
            
                except Exception as e:
                    messagebox.showerror("エラー", str(e))
        
        
            def export_result(self):
                result = self.status_text.get(1.0, tk.END).strip()
                if not result:
                    messagebox.showwarning("警告", "比較結果がありません。")
                    return
        
                try:
                    desktop = os.path.join(os.path.expanduser("~"), "Desktop")
                    now = datetime.now().strftime("%Y%m%d_%H%M%S")
                    file_path = os.path.join(desktop, f"比較結果_{now}.txt")
                    with open(file_path, "w", encoding="utf-8") as f:
                        f.write(result)
                    messagebox.showinfo("保存完了", f"{file_path} に保存しました。")
                except Exception as e:
                    messagebox.showerror("エラー", str(e))
        
        # 実行
        if __name__ == "__main__":
            root = tk.Tk()
        
            ##タイトルバーアイコン表示用
            data = '''R0lGODdhEAAQAIcAAO///9D//sz8/MHo/rn3/6bz/7Tl/Kfc/6PV8qnT/8zO5rjI5bbB3anF+KTB86PO/6LI/6DD75nq/pXq/ZXe9I3d9ZDb+org/4fd+33Y/1Pw/5HS94XO9HbP/5LL95HC+HPX/2HS/0rL/Lm+0rG/4q2/7a27362726G2/aWz2J+lwKyjq4u//ZO6+Yu15Za06IGv6Z6l2Y2r4oqt7XK99WC7/V6+/2q19HSt7n+p2XCo+Wym6GCm+XWg4XGh6XOh2XWd5mui+HCe4k2b4YeZ0YCZ1ZKRonuY14mQr32Qu3uJsXaX2HOTz3iQxHGJto2ImYaIooSEpH+HqXqHrXKGuXCGvXCEwHWEqm6Z3WCR5F+M41KY+VuU2V6H5WqDvG+BtUeD4W98pGt7pGd8uWZ7pmN8t2F7tVt9w2l4vWp2tm16pnB6onR1m2x4mWh4omZ5pmB2smB3pWpzm2lzlmZzlWFxoGBwo2FvmmBun2FtlFxvnl9riFxqj11onl1oi1xohl5ohFxohVt6sVhqj0lorTdvzmBnh15ng15lg11lfVxmglxlgFxlf1xlfltmhltlgltkfltkfFpkgFpkflpke15jhlpjflpjfFtje1pje1tjelljfVtieFpielthellifVliellieFlhe1lheFhifVhielhhelhheVdhe1dheVhgeldgeVVlkFBnl0tkm0hnpkdlpFZjjVRgjFZge1hheFhgeFhgdldgeFdgd1ZgeFZgd1peeldfeldfdlZfe1Zfd1ZfdlZed1ZedlZedVZedFVfelVfd1VedlVedVNff1Nee1ReeFBbeVBbd0pbgDZbpE5aeU5Ze0xWdEZYhUhWdkZTeERRcENRckZQb0BOckBOcT5OczpJbDlJbThJbj9IYzVFajJEaTJDZzhCXjVBXzRBYy9AZiw8Xio7WwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACwAAAAAEAAQAEAI/wCRIQOGK1QnVaIgeULECROuYcg6XZrGCpaVIDU6IBgiQcCAGH+I7UnipIwXIFh6RKBwocEJMoo+aTtXDlslNlGQqFhhzVw1X8GA1aIESE8cKVWEsJAxQ80iS7QCfSGCIwQIHiYWHHBwwwCDN45MOaMz5YgNDSLAMLkiawyaWKiM/TqVaZMyat3EZUtWLFKqW8eG9aKViVEeOXPqwOljB88oXMh+1UrER5ASMz42WMjwgcSINqVsiSnigksWLV1a6CjBIUGTVoce3aHi6tmOLUtgFCAwIQAABX40DXIT5kwhGhh+vCIE4QGKNJImkftmaE2OCh5epIAyDt22aLxWrSNidg1cOG7Sdj0x4g3aLFPBjsk3lgpUpEakmi3LlEvXL2EBAQA7''' 
            root.tk.call('wm', 'iconphoto', root._w, tk.PhotoImage(data=data))
            ####
        
            app = FileComparatorApp(root)
            root.mainloop()
        
    

📦 ダウンロード 🏠 ホームへ