Home » excel » How to Get The Path of an Excel Workbook with Python

How to Get The Path of an Excel Workbook with Python

Posted by: admin May 14, 2020 Leave a comment

Questions:

Pretty much, I have the following code, generating an Excel file.

import xlsxwriter
myName = 'hello.xlsx'
wbk = xlsxwriter.Workbook(myName)

The file hello.xlsx is generated in the same directory, where the Python file is. I can refer to the file path easily like this:

import os
print (os.getcwd()+"\"+myName)

However, is there a way to refer to the path, only using only wbk? Something like wbk.Path from .

How to&Answers:

However, is there a way to refer to the path, only using only wbk

No.

The Workbook object only sees, and stores, the filename that is passed to it by the user. So if the user doesn’t pass the path then the object can’t store it.

I’d suggest using os.getcwd() like in your example.