24-6-23 chatGPT4o로 프로그램 수정

in AVLE 코리아2 months ago

각종 지수를 파악하고 정리하기 위한 프로그램을 만들어 사용하고 있었다.
그런데 자료의 관리와 유지에 조금 귀찮은 점이 있어서 AI에게 수정해 달라고 했더니
자료를 내가 지정한 파일에 써주는 기능으로 바꾸어 주었다.

내가 사용하는 데이터는 커스틈하게 정리해 놓은 것이라 이것을 복사해서 붙여 넣어주면 자동적으로 변환해서 내가 원하는 데이터로 만들어 준다.

프로그램은 아래와 같다
chatGPT4o의 도움을 받았은데 그 이전보다 훨씬 기능이 좋은 것 같다.
프로그래밍 오류도 과거보다 매우 많이 줄었다.
거의 error없이 코딩을 해준다.

import os
import pandas as pd
from tkinter import *
from tkinter import filedialog, messagebox
from datetime import datetime

def paste_from_clipboard():
try:
clipboard_content = root.clipboard_get()
txt.delete('1.0', END)
txt.insert(END, clipboard_content)
except TclError:
pass

def append_data_to_excel():
raw_text = txt.get('1.0', END).strip()
lines = raw_text.split('\n')
today_date = datetime.now().strftime("%Y-%m-%d")
stock_prices = []

for line in lines[1:]:
    columns = line.split('\t')
    if len(columns) > 3:
        name = columns[1].strip()
        price = columns[3].strip()
        if name and price:
            try:
                price = float(price.replace(',', ''))
                stock_prices.append((name, price))
            except ValueError:
                continue

new_data = pd.DataFrame(stock_prices, columns=['종목명', '종가'])
new_data.set_index('종목명', inplace=True)
new_data_transposed = new_data.T
new_data_transposed.index = [today_date]

file_path = filedialog.askopenfilename(
    title="Select Excel File",
    filetypes=[("Excel files", "*.xlsx *.xls")]
)

if not file_path:
    messagebox.showerror("Error", "No file selected")
    return

# 파일 확장자 검사
if not (file_path.endswith('.xlsx') or file_path.endswith('.xls')):
    messagebox.showerror("Error", "Selected file is not an Excel file")
    return

try:
    with pd.ExcelWriter(file_path, engine='openpyxl', mode='a', if_sheet_exists='overlay') as writer:
        startrow = writer.sheets['Sheet1'].max_row if 'Sheet1' in writer.book.sheetnames else 0
        new_data_transposed.to_excel(writer, sheet_name='Sheet1', startrow=startrow, header=False if startrow else True)
    messagebox.showinfo("Success", f"Data appended to Excel successfully.\nFile saved at: {file_path}")
    root.destroy()
except Exception as e:
    messagebox.showerror("Error", f"Error updating Excel file: {e}")

Tkinter GUI 설정

root = Tk()
root.title('Finance Data Processor')
root.geometry('800x600')

txt = Text(root, width=90, height=35)
txt.pack()

paste_button = Button(root, text='Paste from clipboard', command=paste_from_clipboard)
paste_button.pack()
append_button = Button(root, text='Append data and export to Excel', command=append_data_to_excel)
append_button.pack()

root.mainloop()

Sort:  

Thank you, friend!
I'm @steem.history, who is steem witness.
Thank you for witnessvoting for me.
image.png
please click it!
image.png
(Go to https://steemit.com/~witnesses and type fbslo at the bottom of the page)

The weight is reduced because of the lack of Voting Power. If you vote for me as a witness, you can get my little vote.

확실히 전보다 똑똑해지고 있습니다. 학습을 계속 하니 달라지네요.

Coin Marketplace

STEEM 0.16
TRX 0.16
JST 0.030
BTC 58551.10
ETH 2514.90
USDT 1.00
SBD 2.35