Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Archives
Today
Total
관리 메뉴

잡동사니 블로그

[Python] Tkinter와 pyinstaller를 이용해 엑셀 자동화 프로그램 간단 배포 본문

Python

[Python] Tkinter와 pyinstaller를 이용해 엑셀 자동화 프로그램 간단 배포

코딩부대찌개 2024. 4. 9. 00:48

Tkinter

Tkinter는 파이썬의 표준 GUI (그래픽 사용자 인터페이스) 라이브러리로, 파이썬에서 간단한 GUI 애플리케이션을 만들기 위해 사용.

 

Pyinstaller

PyInstaller는 Python 애플리케이션과 모든 종속성을 단일 패키지로 묶음.그리하여 사용자는 Python 인터프리터나 모듈을 설치하지 않고도 패키지된 앱을 실행할 수 있음. PyInstaller는 Python 3.8 이상을 지원하며 numpy, matplotlib, PyQt, wxPython 등과 같은 많은 주요 Python 패키지를 올바르게 번들로 제공함.

https://pyinstaller.org/en/stable/

 

PyInstaller Manual — PyInstaller 6.5.0 documentation

PyInstaller bundles a Python application and all its dependencies into a single package. The user can run the packaged app without installing a Python interpreter or any modules. PyInstaller supports Python 3.8 and newer, and correctly bundles many major P

pyinstaller.org

 

 

기본적인 Tkinter

pip install tkinter
pip install pyinstaller
import tkinter as tk
window = tk.Tk()
window.title("테스트")
window.mainloop()

 

 

지인한테 하나 부탁받아 하게 됐는데 사무실에서 결석자 엑셀 파일 지난달과 이번달 두개 있는데 이걸로 출결을 좀 관리하고 싶은데 엑셀로는 어려워서 Pandas를 이용하게됨.

import tkinter as tk
from tkinter import filedialog, messagebox
import pandas as pd
import sys

def checking(th_month, la_month):
"""
Pandas 사용자 코드
"""
	return df

def generate_excel():
    this_month = str(entry_this_month.get())
    last_month = str(entry_last_month.get())
    df = checking(this_month, last_month)
    file_path = filedialog.asksaveasfilename(defaultextension=".xlsx", filetypes=(("Excel files", "*.xlsx"), ("All files", "*.*")))
    if file_path:
        df.to_excel(file_path, index=False)
        messagebox.showinfo("완료", "엑셀 파일이 생성되었습니다.")
        sys.exit()

def select_file(entry):
    file_path = filedialog.askopenfilename()
    entry.delete(0, tk.END) 
    entry.insert(0, file_path)  

window = tk.Tk()
window.title("출결 탐색")

label_this_month = tk.Label(window, text="이번 달:")
label_this_month.grid(row=0, column=0, padx=5, pady=5, sticky='e')
entry_this_month = tk.Entry(window)
entry_this_month.grid(row=0, column=1, padx=5, pady=5)
select_this_month_button = tk.Button(window, text="파일 선택", command=lambda: select_file(entry_this_month))
select_this_month_button.grid(row=0, column=2, padx=5, pady=5)

label_last_month = tk.Label(window, text="지난 달:")
label_last_month.grid(row=1, column=0, padx=5, pady=5, sticky='e')
entry_last_month = tk.Entry(window)
entry_last_month.grid(row=1, column=1, padx=5, pady=5)
select_last_month_button = tk.Button(window, text="파일 선택", command=lambda: select_file(entry_last_month))
select_last_month_button.grid(row=1, column=2, padx=5, pady=5)

generate_button = tk.Button(window, text="엑셀 생성", command=generate_excel)
generate_button.grid(row=2, column=0, columnspan=3, pady=10)

window.mainloop()

 

코드를 설명하자면 두개의 엑셀 파일을 넣어서 check 이라는 함수가 실행되어 엑셀 파일이 반환되면서 사용자가 이름을 입력해 저장 할 수 있는 코드이다.

이걸이제 배포하자니 Python을 아예 모르는 사용자들은 사용할 수 없기에 이걸 하나의 파일로 생성하기 위하여 Pyinstaller를 사용.

 

Anaconda 프롬프트 또는 터미널을 열고 Pyinstaller를 실행하면 됨.

https://pyinstaller.org/en/v3.4/usage.html

 

Using PyInstaller — PyInstaller 3.4 documentation

Making Linux Apps Forward-Compatible Under Linux, PyInstaller does not bundle libc (the C standard library, usually glibc, the Gnu version) with the app. Instead, the app expects to link dynamically to the libc from the local OS where it runs. The interfac

pyinstaller.org

다양한 옵션들이 있지만 필자가 생각하기에 자주 쓰이는건 

  1. --onefile: 애플리케이션을 하나의 실행 파일로 제작.
  2. --noconsole: 콘솔 창을 표시하지 않고 실행 파일을 제작.
  3. --icon=icon_file.ico: 실행 파일에 아이콘 파일을 포함하여 제작
pyinstaller --onefile --noconsole your_script.py

 

실행하게 되면 dist 폴더에 생성됨.

 

지금까지 일일히 수작업 하셨다는데 잘 사용하시니 다행입니다...