Skip to main content

Creates xlsx from json via openpyxl.

Project description

xlsx_from_json

Creates xlsx from json via openpyxl.

Usage

Create .json file with following structure:

{
    "rows": [
        {
            "cells": [
                {
                    "value": "Sample text",
                    // merge 5x2 cell range
                    "width": 5,
                    "height": 2,
                    // openpyxl style: https://openpyxl.readthedocs.io/en/2.5/styles.html
                    "style": {
                        "font": {
                            "name": "Times New Roman",
                            "size": 12
                        },
                        "border": {
                          "bottom": {
                            // openpyxl border styles: // https://openpyxl.readthedocs.io/en/stable/_modules/openpyxl/styles/borders.html
                            "border_style": "medium",
                            "color": "FFFFFFFF"
                          }
                        }
                    }
                }
            ]
        }
    ],
    "start_column": 3,
    "start_row": 2
}

Create openpyxl workbook via xlsx_from_json function:

import json
from xlsx_from_json import xlsx_from_json

with open("data.json", encoding="utf-8") as f:
    json_data = json.load(f)

wb = xlsx_from_json(json_data)

Created workbook will have values and styles defined above:

sheet = wb.active
assert sheet.cell(row=2, cell=3).value == "Sample text"

You can pass default style to xlsx builder:

from xlsx_from_json import Style
from openpyxl.styles import Font

default_style = Style(font=Font("Times New Roman", 12, True))

wb = xlsx_from_json(json_data, default_style)
sheet = wb.active

assert sheet.cell(row=2, cell=3).font.name == "Times New Roman"
assert sheet.cell(row=2, cell=3).font.size == 12
assert sheet.cell(row=2, cell=3).font.bold

Now you can use workbook according to openpyxl guide.

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

xlsx_from_json-0.1.1.tar.gz (4.3 kB view hashes)

Uploaded Source

Built Distribution

xlsx_from_json-0.1.1-py3-none-any.whl (5.7 kB view hashes)

Uploaded Python 3

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