LoginSignup
0
4

Pythonを使用してExcelの一列に書かれたデータを複数列に分割するGUIアプリを作成

Posted at

何の役に立つのか?

まず一列に書かれたデータがあるとします。

1
データ1
データ2
データ3
データ4
データ5
データ6

これを以下のように3列にしたいという時

1 2 3
データ1 データ2 データ3
データ4 データ5 データ6

手動でやっていては時間の無駄です。
この例では6行のデータを複数列に直すだけなので手動でも簡単にできますが、
100行とか1000行とかのデータを複数列に直そうとするとものすごく時間がかかります。
この問題をPythonでGUIアプリで解決します。

使用方法

私が作成したコードを紹介して、その使い方を説明します。
最後にコードの説明をします。

環境

・Python 3.x
・Tkinter(Python標準ライブラリの一部)
・Pyperclip(インストールが必要: pip install pyperclip)

コード全体

以下が作成したコード全体です。
コピペして保存してみてください。

split_excel_column.py
import tkinter as tk
from tkinter import messagebox
import pyperclip

def split_string_to_columns(input_string, num_columns):
    elements = input_string.split("\r\n")
    formatted_string = ""
    for i in range(0, len(elements), num_columns):
        formatted_string += '\t'.join(elements[i:i+num_columns]) + "\n"
    return formatted_string.strip()

def on_submit():
    input_string = entry.get("1.0", tk.END)
    num_columns = int(column_entry.get())
    if input_string.strip() and num_columns > 0:
        result = split_string_to_columns(input_string, num_columns)
        result_label.config(text=result)
        copy_to_clipboard(result)

def copy_to_clipboard(text):
    pyperclip.copy(text)
    display_text = text.replace("\t", "\\t").replace("\n", "\\n")
    messagebox.showinfo("Copied", f"Text copied to clipboard:\n{display_text}")

root = tk.Tk()
root.title("String to Columns Converter")

entry = tk.Text(root, height=10, width=50)
entry.pack(padx=10, pady=10)

column_label = tk.Label(root, text="Enter number of columns:")
column_label.pack()

column_entry = tk.Entry(root)
column_entry.pack()

submit_button = tk.Button(root, text="Convert to Columns", command=on_submit)
submit_button.pack(pady=10)

result_label = tk.Label(root, text="", anchor='w', justify='left')
result_label.pack(padx=10, pady=10)

root.mainloop()

実行方法

  1. コピペしたPythonコードをターミナルで実行します。
  2. Excelからコピーした列をテキスト入力欄に貼り付けます。
  3. 分割したい列数を指定します。
  4. 「Convert to Columns」ボタンをクリックします。
  5. 結果が下部のラベルに表示され、自動的にクリップボードにコピーされます。

コードの説明

このコードは、Pythonで作成されたグラフィカルユーザーインターフェース(GUI)アプリケーションです。tkinterという標準ライブラリを使用して、ユーザーがテキストを列に分割できるツールを作成しています。具体的な機能は以下の通りです:

  1. ライブラリのインポート

    • tkinter:GUI要素を作成するための標準ライブラリ。
    • tkinter.messagebox:ポップアップメッセージボックスを表示するために使用。
    • pyperclip:クリップボードへのコピー機能を提供。
  2. split_string_to_columns関数

    • 入力された文字列を受け取り、指定された列数に基づいて文字列を分割します。
    • 文字列は改行(\r\n)で分割され、各行はタブ(\t)で結合されます。
  3. on_submit関数

    • ユーザーが「Convert to Columns」ボタンをクリックしたときに呼び出されます。
    • テキストエントリから文字列と列数を取得し、split_string_to_columns関数で処理します。
    • 結果をラベルに表示し、クリップボードにコピーします。
  4. copy_to_clipboard関数

    • 与えられたテキストをクリップボードにコピーします。
    • コピーされたテキストをメッセージボックスで表示します。
  5. GUIコンポーネントの設定

    • メインウィンドウ(root)、テキスト入力欄、ラベル、エントリボックス、ボタン、結果表示用のラベルが配置されています。
  6. アプリケーションの実行

    • root.mainloop()により、アプリケーションが起動し、ユーザーの入力を待ちます。
0
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
4