Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an excel file in a different directory

Tags:

python

if not os.path.isdir("DirectoryName"):
    os.makedirs("DirectoryName")

if not os.path.isfile('FileName.xlsx'):
    wb = openpyxl.Workbook()  
    dest_filename = 'FileName.xlsx' 

I have the following problem: I want to create a directory in the same directory where I have python files and to create the FileName.xlsx in the directory: DirectoryName and I haven't found out how to do that yet.¿Could you help me?

Thanks

like image 450
Jane Avatar asked Mar 13 '23 00:03

Jane


1 Answers

Documentation says you can wb.save(os.path.join('DirectoryName', 'FileName.xlsx'), as_template=False). With dest_filename = 'FileName.xlsx' you just assign value to variable. So try:

if not os.path.isdir("DirectoryName"):
    os.makedirs("DirectoryName")

if not os.path.isfile('FileName.xlsx'):
    wb = openpyxl.Workbook()  
    dest_filename = 'FileName.xlsx' 
    wb.save(os.path.join('DirectoryName', dest_filename), as_template=False)

Note that directory where you file is may not be same as you current directory related to which your path is.

like image 148
George Sovetov Avatar answered Mar 24 '23 00:03

George Sovetov