Converting raw JSON data into polished XLSX reports is a frequent need for Python developers working on analytics dashboards. Aspose.BarCode Cloud SDK for Python provides a powerful library that simplifies barcode generation and file handling in the cloud. In this guide you will learn JSON to XLSX conversion in Python, from setting up the SDK to generating barcode images and exporting the final spreadsheet. By the end you’ll have a reusable script ready for production workloads.
Before You Start: Prerequisites and Installation
To follow this tutorial you need:
- Python 3.7 or newer installed on your machine.
- An IDE or text editor of your choice (VS Code, PyCharm, etc.).
- An Aspose Cloud account with Client Id and Client Secret.
- Internet access for the cloud API calls.
Install the required packages with pip:
pip install aspose-barcode-cloud openpyxl
Download the SDK from the official release page if you prefer a manual install: Download Aspose.BarCode Cloud SDK for Python.
Configure your credentials before making any API calls:
from asposebarcodecloud import Configuration
CLIENT_ID = "YOUR_CLIENT_ID"
CLIENT_SECRET = "YOUR_CLIENT_SECRET"
config = Configuration()
config.host = "https://api.aspose.cloud"
config.api_key["client_id"] = CLIENT_ID
config.api_key["client_secret"] = CLIENT_SECRET
With the environment ready, we can move on to the implementation.
Building It Step by Step: JSON to XLSX Conversion in Python
Step 1: Load the Source JSON File
Read the JSON array that contains the data you want to export.
import json
INPUT_JSON_PATH = "input.json"
with open(INPUT_JSON_PATH, "r", encoding="utf-8") as f:
json_data = json.load(f) # expects a list of dictionaries
Step 2: Prepare the Workbook and Header Row
Create an Excel workbook with OpenPyXL and write column headers.
from openpyxl import Workbook
from openpyxl.utils import get_column_letter
wb = Workbook()
ws = wb.active
ws.title = "Data"
columns = list(json_data[0].keys())
columns.append("BarcodeImage") # extra column for the barcode
for col_idx, col_name in enumerate(columns, start=1):
ws.cell(row=1, column=col_idx, value=col_name)
Step 3: Generate Barcode Images
Use Aspose.BarCode Cloud to create a PNG barcode for each record.
The API reference for BarcodeApi.generate_barcode is available in the API reference.
from asposebarcodecloud import BarcodeApi, GenerateBarcodeRequest, EncodeBarcodeType, ApiClient
from asposebarcodecloud.rest import ApiException
barcode_api = BarcodeApi(ApiClient(config))
def generate_barcode_image(text: str,
barcode_type: EncodeBarcodeType = EncodeBarcodeType.CODE_128) -> bytes:
request = GenerateBarcodeRequest(text=text, type=barcode_type, format="png")
try:
response = barcode_api.generate_barcode(request)
return response.read()
except ApiException as e:
print(f"Error generating barcode for '{text}': {e}")
raise
Step 4: Insert Barcodes into the Worksheet
Cache identical barcodes to avoid redundant API calls, then embed the image.
import io
from openpyxl.drawing.image import Image as XLImage
barcode_cache = {}
for row_idx, record in enumerate(json_data, start=2):
# write regular fields
for col_idx, field_name in enumerate(columns[:-1], start=1):
ws.cell(row=row_idx, column=col_idx, value=record.get(field_name, ""))
# barcode generation / caching
barcode_source = str(record.get("code", f"row{row_idx}"))
img_bytes = barcode_cache.get(barcode_source) or generate_barcode_image(barcode_source)
barcode_cache[barcode_source] = img_bytes
img_stream = io.BytesIO(img_bytes)
img = XLImage(img_stream)
img.width, img.height = 150, 50 # optional size adjustment
img_cell = f"{get_column_letter(len(columns))}{row_idx}"
ws.add_image(img, img_cell)
Step 5: Auto‑Size Columns and Save the Workbook
Finalize the file and write it to disk.
for col_idx in range(1, len(columns) + 1):
column_letter = get_column_letter(col_idx)
ws.column_dimensions[column_letter].width = 20
OUTPUT_XLSX_PATH = "output.xlsx"
wb.save(OUTPUT_XLSX_PATH)
print(f"Conversion complete. XLSX saved to '{OUTPUT_XLSX_PATH}'.")
With these steps the JSON to XLSX conversion is complete, and each row now contains a barcode image generated by Aspose.BarCode.
Full JSON to XLSX Script in Python - Complete Code Example
The following example demonstrates the end‑to‑end implementation of JSON to XLSX conversion using Aspose.BarCode Cloud SDK for Python.
import json
import os
import io
from typing import Dict, Any, List
import asposebarcodecloud
from asposebarcodecloud import (
BarcodeApi,
GenerateBarcodeRequest,
EncodeBarcodeType,
Configuration,
ApiClient,
)
from asposebarcodecloud.rest import ApiException
from openpyxl import Workbook
from openpyxl.drawing.image import Image as XLImage
from openpyxl.utils import get_column_letter
# -------------------- Aspose.BarCode Cloud Setup --------------------
# Replace these placeholders with your actual Aspose Cloud credentials.
CLIENT_ID = "YOUR_CLIENT_ID"
CLIENT_SECRET = "YOUR_CLIENT_SECRET"
config = Configuration()
config.host = "https://api.aspose.cloud"
config.api_key["client_id"] = CLIENT_ID
config.api_key["client_secret"] = CLIENT_SECRET
barcode_api = BarcodeApi(ApiClient(config))
def generate_barcode_image(text: str, barcode_type: EncodeBarcodeType = EncodeBarcodeType.CODE_128) -> bytes:
"""
Calls Aspose.BarCode Cloud to generate a barcode image for the given text.
Returns raw PNG bytes.
"""
request = GenerateBarcodeRequest(
text=text,
type=barcode_type,
format="png"
)
try:
response = barcode_api.generate_barcode(request)
# response is a file-like object; read its content.
return response.read()
except ApiException as e:
print(f"Error generating barcode for '{text}': {e}")
raise
# -------------------- JSON to XLSX Conversion --------------------
INPUT_JSON_PATH = "input.json"
OUTPUT_XLSX_PATH = "output.xlsx"
# Load JSON data (expects a list of dictionaries)
with open(INPUT_JSON_PATH, "r", encoding="utf-8") as f:
json_data: List[Dict[str, Any]] = json.load(f)
if not isinstance(json_data, list):
raise ValueError("JSON root must be an array of objects.")
# Prepare workbook
wb = Workbook()
ws = wb.active
ws.title = "Data"
# Determine column order from first record
columns = list(json_data[0].keys())
# Append a column for the barcode image
barcode_column_name = "BarcodeImage"
columns.append(barcode_column_name)
# Write header row
for col_idx, col_name in enumerate(columns, start=1):
ws.cell(row=1, column=col_idx, value=col_name)
# Cache to avoid regenerating identical barcodes
barcode_cache: Dict[str, bytes] = {}
# Process each JSON record
for row_idx, record in enumerate(json_data, start=2):
# Write regular fields
for col_idx, field_name in enumerate(columns[:-1], start=1):
value = record.get(field_name, "")
ws.cell(row=row_idx, column=col_idx, value=value)
# Generate or retrieve barcode image for a specific field (e.g., "code")
barcode_source = str(record.get("code", f"row{row_idx}"))
if barcode_source in barcode_cache:
img_bytes = barcode_cache[barcode_source]
else:
img_bytes = generate_barcode_image(barcode_source)
barcode_cache[barcode_source] = img_bytes
# Insert barcode image into the last column of the current row
img_stream = io.BytesIO(img_bytes)
img = XLImage(img_stream)
# Adjust image size (optional)
img.width, img.height = 150, 50
img_cell = f"{get_column_letter(len(columns))}{row_idx}"
ws.add_image(img, img_cell)
# Auto‑size columns (basic heuristic)
for col_idx, _ in enumerate(columns, start=1):
column_letter = get_column_letter(col_idx)
ws.column_dimensions[column_letter].width = 20
# Save workbook
wb.save(OUTPUT_XLSX_PATH)
print(f"Conversion complete. XLSX saved to '{OUTPUT_XLSX_PATH}'.")
Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update the file paths (
input.json,output.xlsx, etc.) to match your actual file locations, verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.
Executing the Same Task with cURL and the REST API
If you prefer a pure REST approach, you can achieve the same result with a few cURL commands. The workflow mirrors the Python implementation: obtain an access token, upload the JSON file, generate barcodes, create the XLSX file, and download the result.
1. Authenticate and Get an Access Token
curl -X POST "https://api.aspose.cloud/connect/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET"
The response contains access_token which you will use in subsequent calls.
2. Upload the Source JSON File to Aspose Cloud Storage
curl -X PUT "https://api.aspose.cloud/v3.0/storage/file/input.json" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
--data-binary @input.json
3. Generate a Barcode Image for a Sample Value
curl -X POST "https://api.aspose.cloud/v3.0/barcode/generate" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"text": "SampleCode123",
"type": "Code128",
"format": "png"
}' \
-o barcode.png
4. Convert JSON to XLSX Using Aspose.Cells Cloud
curl -X POST "https://api.aspose.cloud/v3.0/cells/convert?format=xlsx" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"FileName":"input.json"}' \
-o output.xlsx
5. Download the Resulting XLSX File
curl -X GET "https://api.aspose.cloud/v3.0/storage/file/output.xlsx" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-o output.xlsx
These commands illustrate how the same JSON to XLSX conversion can be performed without writing any code. For more details, see the official API documentation.
Configuring Conversion Options for Optimal Results
Aspose.BarCode Cloud offers several parameters you can tweak to suit your project:
- Barcode Type - Choose from
EncodeBarcodeType.CODE_128,EncodeBarcodeType.QR,EncodeBarcodeType.PDF_417, etc. - Image Format - PNG is default, but you can request
jpegorgifby changing theformatfield. - Image Size - Adjust
img.widthandimg.heightin the script to control resolution. - Column Width - Modify
ws.column_dimensions[column_letter].widthto fit longer text.
Example of changing the barcode type and image format:
def generate_barcode_image(text: str,
barcode_type: EncodeBarcodeType = EncodeBarcodeType.QR,
image_format: str = "jpeg") -> bytes:
request = GenerateBarcodeRequest(
text=text,
type=barcode_type,
format=image_format
)
response = barcode_api.generate_barcode(request)
return response.read()
Optimizing Performance for Large JSON Datasets
When dealing with thousands of records, consider these optimizations:
- Stream JSON Instead of Loading Whole File - Use
ijsonto iterate over records without keeping the entire list in memory. - Cache Repeated Barcodes - The
barcode_cachedictionary in the sample code prevents duplicate API calls for identical values. - Resize Images Wisely - Smaller barcode images reduce memory usage and speed up workbook saving.
- Batch Write Rows - OpenPyXL allows writing rows in bulk; grouping writes can lower overhead.
Applying these tips will keep the conversion fast and memory‑efficient even for massive datasets.
Conclusion
JSON to XLSX conversion in Python becomes straightforward when you leverage the power of Aspose.BarCode Cloud SDK for Python. This guide walked you through environment setup, barcode generation, workbook creation, and performance tuning, giving you a solid foundation for building reporting pipelines. Remember that a commercial license is required for production deployments; you can explore pricing options on the product page or obtain a temporary license from the temporary license page. With the provided code and best‑practice recommendations, you’re ready to integrate reliable data export functionality into your applications.
FAQs
-
How can I implement JSON to XLSX conversion in Python without writing my own barcode logic?
Use the Aspose.BarCode Cloud SDK to generate barcodes automatically and combine it with OpenPyXL for Excel creation, as demonstrated in the complete code example. -
What is the recommended way to handle missing fields in the JSON input?
Access dictionary values withrecord.get("field_name", "")to provide a default empty string, preventing runtime errors during worksheet population. -
Are there any limits on the size of the JSON file I can process?
The cloud API itself has no hard limit, but for very large files you should stream the JSON and cache barcodes, as described in the performance section. -
Do I need a separate license for Aspose.Cells when creating the XLSX file?
No. The XLSX generation is performed locally with OpenPyXL, so only the Aspose.BarCode Cloud SDK requires a license for cloud calls.