[英]How do you open an excel file and then click a dialog box before reading the file, from a python script?
我在工作中收到這些 excel 文件發送給我,我試圖從 python 中讀取這些文件以實現自動化。 問題是文件的全部內容不是通過簡單地打開文件來加載的。 打開文件后,會彈出一個對話框,我可以在其中單擊“顯示內容”,然后將加載所有內容。
我需要從 python 中打開 excel 文件,單擊此按鈕,然后讀取內容。 這可能嗎? 我有代碼來簡單地創建一個 tkinter gui 提示來上傳文件,然后使用 openpyxl 從該文件中讀取。 我只需要一種打開文件的方法,然后單擊按鈕。
編輯:同時關閉 Excel 提示符將允許加載內容,因此我可以單擊提示符上的按鈕或關閉提示符,但我需要使用 python 執行此操作,然后等待,然后閱讀表格。
from openpyxl.workbook import Workbook
from openpyxl import load_workbook
from tkinter import *
from tkinter.filedialog import askopenfile
root = Tk()
root.geometry("300x300")
def open_file():
file = askopenfile(mode='r', filetypes = [('Excel Files', '*.xlsx *.xlsm *.csv')])
wb = load_workbook(filename = file.name)
root.destroy()
pass
# I will write code to check for certain values in the file and match here
btn = Button(root, text='Open', command = open_file)
btn.pack(side='top')
mainloop()
我能夠在不打開文件的情況下讀取和寫入文件,這繞過了我收到的錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.