2

I am trying to convert .xlsx to .csv. From xls to csv converter I am hoping to use this code:

import xlrd
import csv
wb = xlrd.open_workbook('Book123.xlsx')
sh = wb.sheet_by_index(0)

your_csv_file = open('your_csv_file.csv', 'w')
wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)

for rownum in range(sh.nrows):
    wr.writerow(sh.row_values(rownum))

your_csv_file.close()

However, when I am trying to execute sh = wb.sheet_by_index(0) I cannot find the sheet.

I also tried wb.sheet_by_name('Sheet1') gets error code : XLRDError: No sheet named <'Sheet1'>. In short, for some reasons, I cannot find any sheets in the file. Can anyone help me reslove this issue?

Thank you very much!

Alice Hsieh
  • 143
  • 3
  • 13
  • 1
    Try getting the sheet names with `wb_sheets=wb.sheet_names()` and use the sheet names you are getting from the result for selecting the sheet like `wb.sheet_by_name(wb_sheets[0])`. – SanthoshSolomon Oct 09 '18 at 02:04
  • It returns an empty list. But I am sure there is a sheet with data in the excel file. – Alice Hsieh Oct 09 '18 at 02:34

1 Answers1

1

Thank you, I found that by saving the file using change the file type to "Excel book (.xlsx)" I was able to find the sheets. The issue was caused by problem handling with the other type of .xlsx "Strick Open XML Spreadsheet(.xlsx)".

Alice Hsieh
  • 143
  • 3
  • 13