how to close the workbook when using xlwt

2,329 views
Skip to first unread message

Shuvayan Das

unread,
Jul 6, 2016, 8:16:07 PM7/6/16
to python-excel
Hello,

I am using the below script to convert some .csv files into a .xls sheet using xlwt:

csv_folder = data_path + "excel_data/"


book
= xlwt.Workbook()
for fil in os.listdir(csv_folder):
    sheet
= book.add_sheet(fil[:-4])
   
with open(csv_folder + fil) as filname:
        reader
= csv.reader(filname)
        i
= 0
       
for row in reader:
           
for j, each in enumerate(row):
                sheet
.write(i, j, each)
            i
+= 1


book
.save(data_path + "Output.xls")

 And then for further use I am trying to convert it into xlsx format using the code below:

# Convert xls workbook to xlsx:
import win32com.client as win32
fname_xl
= data_path + "Output.xls"
excel
= win32.gencache.EnsureDispatch('Excel.Application')
wb
= excel.Workbooks.Open(fname_xl)
wb
.SaveAs(fname_xl+"x", FileFormat = 51)    #FileFormat = 51 is for .xlsx extension
wb
.Close()                          

But here I am getting an error in excel.Workbooks.Open : workbook is already open.
is there an option like book.close() in xlwt so that I can close the workbook before I open it again in the same session??

Adrian Klaver

unread,
Jul 7, 2016, 10:07:38 AM7/7/16
to python...@googlegroups.com
On 07/06/2016 09:45 AM, Shuvayan Das wrote:
> Hello,
>
> I am using the below script to convert some .csv files into a .xls sheet
> using xlwt:
>
> |
> csv_folder =data_path +"excel_data/"
>
>
> book =xlwt.Workbook()
> forfil inos.listdir(csv_folder):
> sheet =book.add_sheet(fil[:-4])
> withopen(csv_folder +fil)asfilname:
> reader =csv.reader(filname)
> i =0
> forrow inreader:
> forj,each inenumerate(row):
> sheet.write(i,j,each)
> i +=1
>
>
> book.save(data_path +"Output.xls")
>
> |
> And then for further use I am trying to convert it into xlsx format
> using the code below:
>
> |
> # Convert xls workbook to xlsx:
> importwin32com.client aswin32
> fname_xl =data_path +"Output.xls"
> excel =win32.gencache.EnsureDispatch('Excel.Application')
> wb =excel.Workbooks.Open(fname_xl)
> wb.SaveAs(fname_xl+"x",FileFormat=51) #FileFormat = 51 is for .xlsx
> extension
> wb.Close()
> |
>
> But here I am getting an error in excel.Workbooks.Open : workbook is
> already open.
> is there an option like book.close() in xlwt so that I can close the
> workbook before I open it again in the same session??

That does not seem to be the problem:

In [1]: import xlwt

In [2]: wb = xlwt.Workbook()

In [3]: wb.add_sheet('sheet1')
Out[3]: <xlwt.Worksheet.Worksheet at 0xb5ee872c>

In [4]: wb.save('test.xls')

In [5]: open('test.xls')
Out[5]: <open file 'test.xls', mode 'r' at 0xb5f39de0>


I would look at the win32 code. In particular do you get the error when
you do?:

wb =excel.Workbooks.Open(fname_xl)

or

wb.SaveAs(fname_xl+"x",FileFormat=51)

Another option is to work in xlsx directly:

https://xlsxwriter.readthedocs.io/


--
Adrian Klaver
adrian...@aklaver.com

Adrian Klaver

unread,
Jul 7, 2016, 10:17:50 AM7/7/16
to python...@googlegroups.com
On 07/06/2016 09:45 AM, Shuvayan Das wrote:
> Hello,
>
> I am using the below script to convert some .csv files into a .xls sheet
> using xlwt:
>
> |
> csv_folder =data_path +"excel_data/"
>
>
> book =xlwt.Workbook()
> forfil inos.listdir(csv_folder):
> sheet =book.add_sheet(fil[:-4])
> withopen(csv_folder +fil)asfilname:


Just noticed, and not sure if it makes a difference, but I would do:

with open(csv_folder + fil, 'rb')

> reader =csv.reader(filname)
> i =0
> forrow inreader:
> forj,each inenumerate(row):
> sheet.write(i,j,each)
> i +=1
>
>
> book.save(data_path +"Output.xls")
>
> |
> And then for further use I am trying to convert it into xlsx format
> using the code below:
>
> |
> # Convert xls workbook to xlsx:
> importwin32com.client aswin32
> fname_xl =data_path +"Output.xls"
> excel =win32.gencache.EnsureDispatch('Excel.Application')
> wb =excel.Workbooks.Open(fname_xl)
> wb.SaveAs(fname_xl+"x",FileFormat=51) #FileFormat = 51 is for .xlsx
> extension
> wb.Close()
> |
>
> But here I am getting an error in excel.Workbooks.Open : workbook is
> already open.
> is there an option like book.close() in xlwt so that I can close the
> workbook before I open it again in the same session??



--
Adrian Klaver
adrian...@aklaver.com

John Yeung

unread,
Jul 7, 2016, 10:18:09 AM7/7/16
to python-excel
On Thu, Jul 7, 2016 at 10:07 AM, Adrian Klaver
<adrian...@aklaver.com> wrote:
> On 07/06/2016 09:45 AM, Shuvayan Das wrote:
>> And then for further use I am trying to convert it into xlsx format
>> using the code below:
>
> Another option is to work in xlsx directly:
>
> https://xlsxwriter.readthedocs.io/

If the desired format is .xlsx anyway, that should be the first option.

Anyone who is currently using xlwt but wants to output .xlsx should
simply switch to XlsxWriter.

John Y.
Reply all
Reply to author
Forward
0 new messages