Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert JSON to XLS in Python

Tags:

python

json

xls

Does anyone know how can I convert JSON to XLS in Python?

I know that it is possible to create xls files using the package xlwt in Python.

What if I want convert a JSON data convert to XLS file directly?

Is there a way to archive this?

like image 230
fakelbst Avatar asked Mar 13 '13 07:03

fakelbst


People also ask

How do I convert JSON to Excel?

On the “Data” tab, from the “Get & Transform Data” section, select Get Data > From File > From JSON. You will see your computer's standard “Import” window. Here, open the folder where your JSON file is located. Double-click the file to connect it to Excel.

How do I convert JSON data to CSV in Python?

To convert JSON to CSV in Python, use the pandas to_csv() function. The to_csv() is a Pandas library function that writes objects to a comma-separated values (csv) file. The first step to convert json to csv is to read json data using the Pandas read_json() function and then convert it to csv using to_csv() function.


4 Answers

Using pandas (0.15.1) and openpyxl (1.8.6):

import pandas
pandas.read_json("input.json").to_excel("output.xlsx")
like image 136
Bruno Lopes Avatar answered Sep 30 '22 19:09

Bruno Lopes


I usually use tablib for this use. Its pretty simple to use: https://pypi.python.org/pypi/tablib/0.9.3

like image 34
GodMan Avatar answered Sep 30 '22 19:09

GodMan


If your json file is stored in some directory then,

import pandas as pd
pd.read_json("/path/to/json/file").to_excel("output.xlsx")

If you have your json within the code then, you can simply use DataFrame

json_file = {'name':["aparna", "pankaj", "sudhir", "Geeku"],'degree': ["MBA", "BCA", "M.Tech", "MBA"],'score':[90, 40, 80, 98]}
df = pd.DataFrame(json_file).to_excel("excel.xlsx")
like image 26
laplace Avatar answered Sep 30 '22 17:09

laplace


In case someone wants to do output to Excel as a stream using Flask-REST

Pandas versions:

json_payload = request.get_json()

with NamedTemporaryFile(suffix='.xlsx') as tmp:

    pandas.DataFrame(json_payload).to_excel(tmp.name)

    buf = BytesIO(tmp.read())

    response = app.make_response(buf.getvalue())
    response.headers['content-type'] = 'application/octet-stream'

    return response

and OpenPyXL version:

keys = []
wb = Workbook()
ws = wb.active

json_data = request.get_json()

with NamedTemporaryFile() as tmp:

    for i in range(len(json_data)):
        sub_obj = json_data[i]
        if i == 0:
            keys = list(sub_obj.keys())
            for k in range(len(keys)):
                ws.cell(row=(i + 1), column=(k + 1), value=keys[k]);
        for j in range(len(keys)):
            ws.cell(row=(i + 2), column=(j + 1), value=sub_obj[keys[j]]);
    wb.save(tmp.name)

    buf = BytesIO(tmp.read())

    response = app.make_response(buf.getvalue())
    response.headers['content-type'] = 'application/octet-stream'

    return response
like image 1
JackTheKnife Avatar answered Sep 30 '22 18:09

JackTheKnife