Skip to main content

JSON to excel convertor

Project description

JSON to excel converter

A package that converts json to CSV, excel or other table formats

Sample output

Simple json

[
  {
    "col1": "val1",
    "col2": "val2" 
  }
]

the generated CSV/excel is:

col1          col2
==================
val1          val2

Nested json

[
  {
    "col1": "val1",
    "col2": {
      "col21": "val21",
      "col22": "val22"
    }
  }
]

the generated CSV/excel is (in excel, col2 spans two cells horizontally):

col1          col2
              col21         col22
=================================
val1          val21         val22

json with array property

[
  {
    "col1": "val1",
    "col2": [
      {
        "col21": "val21"
      },
      {
        "col21": "val22"
      }
    ]
  }
]

the generated CSV/excel is (in excel, col2 spans two cells horizontally):

col1          col2         
              col21         col21
=================================
val1          val21         val22

Installation

pip install json-excel-converter[extra]

where extra is:

  • xlsxwriter to use the xlsxwriter

Usage

Simple usage

from json_excel_converter import Converter 
from json_excel_converter.xlsx import Writer

data = [
    {'a': [1], 'b': 'hello'},
    {'a': [1, 2, 3], 'b': 'world'}
]

conv = Converter()
conv.convert(data, Writer(file='/tmp/test.xlsx'))

Streaming usage with restarts

from json_excel_converter import Converter, LinearizationError 
from json_excel_converter.csv import Writer

conv = Converter()
writer = Writer(file='/tmp/test.csv')
while True:
    try:
        data = get_streaming_data()     # custom function to get iterator of data
        conv.convert_streaming(data, writer)
        break
    except LinearizationError:
        pass

Arrays

When the first row is processed, the library guesses the columns layout. In case of arrays, a column (or more columns if the array contains json objects) is created for each of the items in the array, as shown in the example above.

On subsequent rows the array might contain more items. The library reacts by adjusting the number of columns in the layout and raising LinearizationError as previous rows might be already output.

Converter.convert_streaming just raises this exception - it is the responsibility of caller to take the right action.

Converter.convert captures this error and restarts the processing. In case of CSV this means truncating the output file to 0 bytes and processing the data again. XLSX writer caches all the data before writing them to excel so the restart just means discarding the cache.

If you know the size of the array in advance, you should pass it in options. Then no processing restarts are required and LinearizationError is not raised.

from json_excel_converter import Converter, Options
from json_excel_converter.xlsx import Writer

data = [
   {'a': [1]},
   {'a': [1, 2, 3]}
]
options = Options()
options['a'].cardinality = 3

conv = Converter(options=options)
writer = Writer(file='/tmp/test.xlsx')
conv.convert(data, writer)
# or
conv.convert_streaming(data, writer)    # no exception occurs here

XLSX Formatting

Cell format

XLSX writer enables you to format the header and data by passing an array of header_formatters or data_formatters. Take these from json_excel_converter.xlsx.formats package or create your own.

from json_excel_converter import Converter

from json_excel_converter.xlsx import Writer
from json_excel_converter.xlsx.formats import LastUnderlined, Bold, \
    Centered, Format

data = [
    {'a': 'Hello'},
    {'a': 'World'}
]

w = Writer('/tmp/test3.xlsx',
           header_formats=(
               Centered, Bold, LastUnderlined,
               Format({
                   'font_color': 'red'
               })),
           data_formats=(
               Format({
                   'font_color': 'green'
               }),)
           )

conv = Converter()
conv.convert(data, w)

See https://xlsxwriter.readthedocs.io/format.html for details on formats in xlsxwriter

Column widths

Pass the required column widths to writer:

w = Writer('/tmp/test3.xlsx', column_widths={
    'a': 20
})

Width of nested data can be specified as well:

data = [
    {'a': {'b': 1, 'c': 2}}
]

w = Writer('/tmp/test3.xlsx', column_widths={
    'a.b': 20,
    'a.c': 30,
})

To set the default column width, pass it as DEFAULT_COLUMN_WIDTH property:

w = Writer('/tmp/test3.xlsx', column_widths={
    DEFAULT_COLUMN_WIDTH: 20
})

Row heights

Row heights can be specified via the row_heights writer option:

w = Writer('/tmp/test3.xlsx', row_heights={
    DEFAULT_ROW_HEIGHT: 20,     # a bit taller rows
    1: 40                       # extra tall header
})

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

json_excel_converter-1.1.0.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

json_excel_converter-1.1.0-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file json_excel_converter-1.1.0.tar.gz.

File metadata

  • Download URL: json_excel_converter-1.1.0.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.7.1 Linux/4.15.0-1028-gcp

File hashes

Hashes for json_excel_converter-1.1.0.tar.gz
Algorithm Hash digest
SHA256 79282bac996c8211bfb261d8c346470f633421d823c8c72243fe7f664244edd7
MD5 906ad5698505d141fe0bd18ad0a6eb02
BLAKE2b-256 a0f0d5103754c2a3771e898cc1d116f8e0fa0c264772f66437f50e7947a2c30d

See more details on using hashes here.

File details

Details for the file json_excel_converter-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for json_excel_converter-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1a70d1ba73d46b24ba160c272f698b1cdbd2cbbaba422ce752d9db907f40619e
MD5 be18c91154f3baa16364c8fe4577fe9b
BLAKE2b-256 1a4de5b0e080722f4657208f24740ebeedf7287373a1802a59799b237479f2b6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page