컴퓨터/Python

python pandas 매입세금계산서 파일 합치기 messagebox 완료 메시지 띄우기

풍경소리^^ 2023. 10. 24. 14:26

# hometax_pandas.py

import pandas as pd
import os
import win32api

url = "B:\\python\\hometax\\data\\"
os.listdir(url)
os.chdir(url)

df = pd.DataFrame([])
excel_list = []
for j in os.listdir(url):
    if j == "hometax_pandas_xlsxWriter.xlsx" or j == "txt_hometax1.txt":
        continue
    excel_list.append(j)

dropcols = ['전자세금계산서분류', '전자세금계산서종류', '발급유형', '비고', '영수/청구 구분',
            '공급자 이메일', '공급받는자 이메일1', '공급받는자 이메일2', '품목일자', '품목명', '품목규격', '품목수량',
            '품목단가', '품목공급가액', '품목세액', '품목비고']
if len(excel_list) != 0:
    df_total = pd.read_excel(excel_list[0], skiprows=5)
#     cnt = df_total.shape[0]  # 행
    for item in excel_list[1:]:
        df = pd.read_excel(item, skiprows=5)
        df_total = pd.concat([df_total, df], axis=0)
    
    df_total = df_total.drop(columns=dropcols)
    
    df_total.to_excel(url + "/hometax_pandas_xlsxWriter.xlsx", index=False, sheet_name="Sheet1")
    win32api.MessageBox(0, "통합파일 생성 완료!", "파일 생성")
else:
    print("통합할 파일이 없습니다.")

http://localhost:8888/notebooks/filesheet합치기.ipynb--------------------win32api

import pandas as pd
import os
import win32api

url = r"B:\python\hometax\data"
target_file = r"\hometax_pandas_xlsxWriter.xlsx"

os.chdir(url)

df = pd.DataFrame([])
excel_list = []
files = os.listdir(url)

dropcols = ['전자세금계산서분류', '전자세금계산서종류', '발급유형', '비고', '영수/청구 구분',
            '공급자 이메일', '공급받는자 이메일1', '공급받는자 이메일2', '품목일자', '품목명', '품목규격', '품목수량',
            '품목단가', '품목공급가액', '품목세액', '품목비고']

for file in files:
#     if file == "hometax_pandas_xlsxWriter.xlsx" or file == "txt_hometax1.txt":
#         continue
    if file.endswith('.xls'):
        excel_list.append(file)

if len(excel_list) != 0:
    df_total = pd.read_excel(excel_list[0], skiprows=5) # 첫번째 파일
#     cnt = df_total.shape[0]  # 행
    for item in excel_list[1:]: # 두번째 파일 부터
        df = pd.read_excel(item, skiprows=5)
        df_total = pd.concat([df_total, df], axis=0)
    
    df_total = df_total.drop(columns=dropcols)
    
    df_total.to_excel(url + target_file, index=False, sheet_name="Sheet1")
    win32api.MessageBox(0, "통합파일 생성 완료!", "파일 생성")
else:
    print("통합할 파일이 없습니다.")

hometax_pandas.py--------------------pyautogui

# 가상환경 들어가지말고 3.9.12('py39_32':conda)
import pandas as pd
import numpy as np
import os
import glob
import time
# import win32api # messagebox
import pyautogui # messagebox

# location = os.getcwd()
location = 'b:/python/hometax/data'
# path = os.path.dirname('b:/Python/hometax/data/')
os.chdir(location)
excels = glob.glob('*.xls*')
print(excels)

excel_list = []
for excel in excels:
    if excel == "hometax_pandas_xlsxWriter.xlsx":
        continue
    excel_list.append(excel)

# start = time.time()  # 시작 시간 저장
dropcols = ['전자세금계산서분류', '전자세금계산서종류', '발급유형', '비고', '영수/청구 구분',
            '공급자 이메일', '공급받는자 이메일1', '공급받는자 이메일2', '품목일자', '품목명', '품목규격', '품목수량',
            '품목단가', '품목공급가액', '품목세액', '품목비고']
if len(excel_list) != 0:
    df_total = pd.read_excel(excel_list[0], skiprows=5)
    cnt = df_total.shape[0]  # 행
    #     df_total.loc[cnt+1] = np.nan # 빈행 추가
    for item in excel_list[1:]:
        df = pd.read_excel(item, skiprows=5)
        cnt = df.shape[0]  # 행
        #         df.loc[cnt+1] = np.nan # 빈행 추가

        df_total = pd.concat([df_total, df], axis=0)

    df_total = df_total.drop(columns=dropcols)

    df_total.to_excel(location + "/hometax_pandas_xlsxWriter.xlsx", index=False, sheet_name="Sheet1")
    # win32api.MessageBox(0, "통합파일 생성 완료!", "파일 생성")
    pyautogui.alert("통합파일 생성 완료!", "파일 생성", "확인")
#     print("time :", time.time() - start)  # 현재시각 - 시작시간 = 실행 시간

else:
    print("통합할 파일이 없습니다.")

hometax_pandas_qmessagebox.py--------------------pyside6 pyqt5

import pandas as pd
import numpy as np
import os
import glob
from PySide6.QtWidgets import QApplication, QMessageBox
import sys

class MessageBox(QMessageBox):
    def __init__(self, parent=None):
        super(MessageBox, self).__init__()

        self.setWindowTitle('파일 생성')
        # self.setFixedSize(200, 100)

        self.setText("파일 생성 완료!")

        # Add an "Ok" button to close the dialog
        ok_button = self.addButton(QMessageBox.Ok)
        ok_button.setText('완료')

def create_and_display_message_box():
    location = 'b:/python/hometax/data'
    os.chdir(location)
    excels = glob.glob('*.xls*')
    print(excels)

    excel_list = []
    for excel in excels:
        if excel == "hometax_pandas_xlsxWriter.xlsx":
            continue
        excel_list.append(excel)

    dropcols = ['전자세금계산서분류', '전자세금계산서종류', '발급유형', '비고', '영수/청구 구분',
                '공급자 이메일', '공급받는자 이메일1', '공급받는자 이메일2', '품목일자', '품목명', '품목규격', '품목수량',
                '품목단가', '품목공급가액', '품목세액', '품목비고']

    if len(excel_list) != 0:
        df_total = pd.read_excel(excel_list[0], skiprows=5)
        cnt = df_total.shape[0]

        for item in excel_list[1:]:
            df = pd.read_excel(item, skiprows=5)
            cnt = df.shape[0]
            df_total = pd.concat([df_total, df], axis=0)

        df_total = df_total.drop(columns=dropcols)

        df_total.to_excel(location + "/hometax_pandas_xlsxWriter.xlsx", index=False, sheet_name="Sheet1")
        
        # Display the message box after file creation
        app = QApplication(sys.argv)
        msgBox = MessageBox()
        msgBox.show()
        sys.exit(app.exec())

    else:
        print("통합할 파일이 없습니다.")

# Call the function to create and display the message box
create_and_display_message_box()