iT邦幫忙

0

tkinter、 openpyxl開啟excel文件修改儲存(python)

  • 分享至 

  • xImage
想問一下程式讀不到excel問題在那邊呢
import tkinter as tk
from tkinter import filedialog
import openpyxl as xl


def openbook():
    fpath = filedialog.askopenfilename()
    print(fpath)
    if fpath:
        pass


wino = tk.Tk()
wino.geometry('200x100')
btno = tk.Button(wino, text='開啟', command=openbook)
btno.pack(side=tk.TOP, expand=tk.YES)
wino.mainloop()


def win_openxls(fpath):
    global wino
    wino.destroy()
    savepath = fpath
    w = xl.load_workbook(fpath)
    name = w.sheetnames
    print(name)
    sheet = w[name[0]]
    n = sheet.max_row
    col = sheet.max_column


def viewclick(event):
    global nwin
    global tree
    global enty
    global sitem
    global colint
    for item in tree.selection():
        ttext = tree.item(item, 'values')
        sitem = item

    col = tree.identify_column(event.x)
    colint = int(str(col.replace('#', '')))

    nwin = tk.Tk()  # 編輯視窗
    nwin.geometry("260x100")
    label1 = tk.Label(nwin, text="修改:")
    label1.pack(side=tk.LEFT, fill=None)
    enty = tk.Text(nwin, width=300, height=300, wrap=tk.WORD)
    enty = tk.Entry(nwin)
    enty.pack(side=tk.LEFT, fill=None)
    btn = tk.Button(nwin, text='確認', command=getv)
    btn.pack(side=tk.LEFT, padx=6, ipadx=6)
    enty.insert('end', ttext[colint-1])  # 編輯框顯示值
    openwin.append(nwin)

 #   if len(openwin) > 1:
 #       openwin.pop(0).destroy()
 #   nwin.protocol('WM_DELETE_WINDOW', initopenwin)  # 繫結時件,關閉窗清除變數值
 #   nwin.mainloop()


def getv():
    global nwin
    global enty
    global tree
    global sitem
    global colint
    global openwin
    editxt = enty.get()
    tree.set(sitem, (colint-1), editxt)
    openwin = []
    nwin.destroy()


def savebook():
    global w
    global tree
    global titles
    global savepath

    ws = w.create_sheet('change1')
    ws.append(titles)
    for itm in tree.get_children():
        ws.append(tree.item(itm)['values'])
        w.save(savepath)
     #   messagebox.showinfo('提示', '儲存成功')

froce iT邦大師 1 級 ‧ 2022-11-30 09:06:34 檢舉
你是哪抄來的,連自己要import什麼都不清楚...
import openpyxl as xl
tk.messagebox

initopenwin那個應該是個自訂的函式,拿來定義關閉主要視窗時要幹嘛的。
4A790059 iT邦新手 5 級 ‧ 2022-11-30 10:00:35 檢舉
https://www.gushiciku.cn/pl/pTOi/zh-tw 我是在這個網站上看的欸
win895564 iT邦新手 1 級 ‧ 2022-11-30 17:43:52 檢舉
你有單獨測試過讀取excel功能嗎
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答