capser
capser

Reputation: 2635

Python save XLSX to CSV

I am not a python user. However, I am sick of saving Excel files to CSV manually, and everybody hates Perl at this shop. I can't get Spreadsheet::XLSX to work in this work environment. They only use Python.

The python version is 2.4.

 #!/usr/bin/python

import openpyxl
import csv

wb = openpyxl.load_workbook('DailySnapshot.xlsx')
sh = wb.get_active_sheet()
with open('test.csv', 'wb') as f:
    c = csv.writer(f)
    for r in sh.rows:
        c.writerow([cell.value for cell in r])

The DailySnapshot.xlxs is saved in the same directory a the script. It is a one page excel spreadsheet, and the sheet is called 'Table1'. I figured I would name the CSV file test.csv. This is the error it throws.

File "./secondPyTry.py", line 8 with open('test.csv', 'wb') as f: ^ SyntaxError: invalid syntax

Upvotes: 2

Views: 1192

Answers (1)

francisco sollima
francisco sollima

Reputation: 8338

As it was said in the comments, Python 2.4 does not wupport with. You should open the file like this instead:

f = open('test.csv', 'wb')
c = csv.writer(f)
for r in sh.rows:
    c.writerow([cell.value for cell in r])
f.close()

Upvotes: 2

Related Questions