Python Forum
Issue with a script to convert xls to json
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue with a script to convert xls to json
#1
Hi there,

It seems that I have an issue with this python 2 script (from https://github.com/paceaux/xlsx-to-json) translated to py3.

Any idea on how I could improve it?
It return the following
Quote:Enter the path to the filename -> C:\......\MS1.xls
Traceback (most recent call last):
File "C:\......xls2json.py", line 69, in <module>
main()
File "C:\......\xls2json.py", line 63, in main
output.write(json.dumps(workbookdata, sort_keys=True, indent=2, separators=(',', ": ")))
TypeError: a bytes-like object is required, not 'str'


import xlrd
import xlwt
import json
import os.path
import datetime

def getColNames(sheet):
	rowSize = sheet.row_len(0)
	colValues = sheet.row_values(0, 0, rowSize )
	columnNames = []

	for value in colValues:
		columnNames.append(value)

	return columnNames

def getRowData(row, columnNames):
	rowData = {}
	counter = 0

	for cell in row:
		# check if it is of date type print in iso format
		if cell.ctype==xlrd.XL_CELL_DATE:
			rowData[columnNames[counter].lower().replace(' ', '_')] = datetime.datetime(*xlrd.xldate_as_tuple(cell.value,0)).isoformat()
		else:
			rowData[columnNames[counter].lower().replace(' ', '_')] = cell.value
		counter +=1

	return rowData

def getSheetData(sheet, columnNames):
	nRows = sheet.nrows
	sheetData = []
	counter = 1

	for idx in range(1, nRows):
		row = sheet.row(idx)
		rowData = getRowData(row, columnNames)
		sheetData.append(rowData)

	return sheetData

def getWorkBookData(workbook):
	nsheets = workbook.nsheets
	counter = 0
	workbookdata = {}

	for idx in range(0, nsheets):
		worksheet = workbook.sheet_by_index(idx)
		columnNames = getColNames(worksheet)
		sheetdata = getSheetData(worksheet, columnNames)
		workbookdata[worksheet.name.lower().replace(' ', '_')] = sheetdata

	return workbookdata

def main():
	filename = input("Enter the path to the filename -> ")
	if os.path.isfile(filename):
		workbook = xlrd.open_workbook(filename)
		workbookdata = getWorkBookData(workbook)
		output = \
		open((filename.replace("xlsx", "json")).replace("xls", "json"), "wb")
		output.write(json.dumps(workbookdata, sort_keys=True, indent=2,  separators=(',', ": ")))
		output.close()
		print("%s was created" %output.name)
	else:
		print("Sorry, that was not a valid filename")

main()
Reply
#2
At line 62, try to replace the "wb" mode with "w".
Reply
#3
worked like a charm. thanks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Script to convert Json to CSV file chvsnarayana 8 2,496 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Convert Json to table format python_student 2 5,460 Sep-28-2022, 12:48 PM
Last Post: python_student
  Convert nested sample json api data into csv in python shantanu97 3 2,808 May-21-2022, 01:30 PM
Last Post: deanhystad
  Convert python dataframe to nested json kat417 1 6,324 Mar-18-2022, 09:14 PM
Last Post: kat417
Question convert autohotkey script to python / macro - press key when pixel get colour willson94d 1 3,640 Jan-01-2022, 08:13 PM
Last Post: Yoriz
Question convert unlabeled list of tuples to json (string) masterAndreas 4 7,459 Apr-27-2021, 10:35 AM
Last Post: masterAndreas
  Convert string to JSON using a for loop PG_Breizh 3 2,964 Jan-08-2021, 06:10 PM
Last Post: PG_Breizh
  How to convert what appears to be a JSON file to CSV NewBeie 4 2,512 Aug-28-2020, 04:45 PM
Last Post: Larz60+
  Convert .py script to dll file and .exe srikanthpython 0 3,127 Aug-20-2020, 06:01 PM
Last Post: srikanthpython
  [split] script: remove all "carriage return" from my json variable pete 2 2,789 May-05-2020, 03:22 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020