How to provide path for saving Excel file

Hi,

I am using below code to create an Excel file. I have copied the code from below website.

This code is working however files are saved at a default path. How do I provide path to save the file. Can anyone please help me in this.

Writing to an excel

sheet using Python

import xlwt
from xlwt import Workbook

Workbook is created

wb = Workbook()

add_sheet is used to create sheet.

sheet1 = wb.add_sheet(‘Sheet 1’)

sheet1.write(1, 0, ‘Sachin’)
sheet1.write(2, 0, ‘Brian’)
sheet1.write(3, 0, ‘Rahul’)
sheet1.write(4, 0, ‘Anil’)
sheet1.write(5, 0, ‘Robin Singh’)
sheet1.write(0, 1, ‘Sachin’)
sheet1.write(0, 2, ‘Brian’)
sheet1.write(0, 3, ‘Rahul’)
sheet1.write(0, 4, ‘Anil’)
sheet1.write(0, 5, ‘Robin Singh’)

#How do I add folder path here
wb.save(‘Added by Python.xls’)

You just specify the path as a string argument in save, as you do.

from xlwt import Workbook
wb = Workbook()
sheet1 = wb.add_sheet("Sheet 1")
wb.save("/tmp/workbook.xls")

The above saves a workbook.xls file in “/tmp” directory on my machine. So the full path is “/tmp/workbook.xls”.

However, the save() method will not create the directory if it doesn’t exist. In such case you need to create the destination directory before calling save(). You can do it using os.mkdir or os.makedirs.

1 Like

Hi @pymc , thanks a lot for the help. Have a nice day ahead. :slight_smile: