Select path to Excel file using TKIinter

Question:

i am trying to select the path to an Excel file using TKinter.
The Idea is to have two browser dialogs opening. One asking to select an Excel file an the other asking to define the destination folder.

import numpy as np
import openpyxl
import matplotlib.pyplot as plt
#import plotly.graph_objects as go
import tkinter
from tkinter import filedialog
import os

root = tkinter.Tk()
root.withdraw()

def open_file():
   file = filedialog.askopenfile(mode='r', filetypes=[('Excel Files', '*.xlsx')])
   filepath = os.path.abspath(file.name)

def search_for_file_path_dest ():
    currdir = os.getcwd()
    tempdir = filedialog.askdirectory(parent=root, initialdir=currdir, title='Please select the destination directory')
    if len(tempdir) > 0:
        print ("You chose: %s" % tempdir)
    return tempdir

filepath = open_file()
file_path_variabledest = search_for_file_path_dest()

print(filepath)
print(file_path_variabledest)

The destination folder is selected correctly but the path to the excel file (filepath) is "None".
What am I doing wrong?

Thnak you

Asked By: Dedder

||

Answers:

Function open_file was nothing returned. You must return filepath.

def open_file():
   file = filedialog.askopenfile(mode='r', filetypes=[('Excel Files', '*.xlsx')])
   filepath = os.path.abspath(file.name)
   return filepath
Answered By: FirstShanti
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.