Search code examples
pythonpandasdataframepathexport-to-excel

From dataframe to Excel in Desktop


I exported dataframe from Python to Excel. I have no problem, but I have a question if someone can help me. Can I change the path of the file without having to go through personal folders like "crisf" in my case, to save it to the desktop? I am looking for a generic route that can be applied to all computers

xlwriter= pd.ExcelWriter(r'C:\Users\crisf\Desktop\OHH + GIT + M3.xls')

Solution

  • I doubt that all computers will have user called "crisf". Instead use this and get universal path (path contains user’s home directory):

    import os
    ***some code
    xlwriter= pd.ExcelWriter(os.path.expanduser("~/Desktop") + path of folder on desktop + file name)
    

    I run print(os.path.expanduser("~\Desktop")) command on my PC and got: C:\Users\korne\Desktop