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