Skip to content

Instantly share code, notes, and snippets.

@polikeiji
Last active August 29, 2015 14:23
Show Gist options
  • Save polikeiji/a00455c83fdbcdb8f110 to your computer and use it in GitHub Desktop.
Save polikeiji/a00455c83fdbcdb8f110 to your computer and use it in GitHub Desktop.
エクセルをJSONに直すPythonスクリプト
import xlrd
from collections import OrderedDict
import simplejson as json
excel_path = raw_input('input path> ')
json_path = raw_input('output json path> ')
wb = xlrd.open_workbook(excel_path)
sheets = OrderedDict()
for sheet_name in wb.sheet_names():
sh = wb.sheet_by_name(sheet_name)
rows = []
header_row = sh.row_values(0)
for rownum in range(1, sh.nrows):
row_values = sh.row_values(rownum)
row = OrderedDict()
for column in range(0, len(header_row)) :
row[header_row[column]] = row_values[column]
rows.append(row)
sheets[sheet_name] = rows
json_string = json.dumps(sheets)
with open(json_path, 'w') as f:
f.write(json_string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment