E
E
exctac2018-10-24 12:20:12
Django
exctac, 2018-10-24 12:20:12

XlsxWriter. Description of the workbook/sheets/tables as a class?

Good afternoon.
There is such a package XlsxWriter for exporting data to excel. I needed to generate all kinds of reports, which were different tables, with different structures, many sheets, etc., based on json data. After reading the documentation, I realized that creating a workbook, sheets, tables, formatting cells, etc. takes place in a rather inconvenient form, namely, you need to create a workbook instance, add sheets, then insert a cell/row/column/table into each sheet, set certain coordinates, while the coordinates need to be known and remembered so that one table does not overlap another. And so each table. The result is a lot of consistent and repetitive code. I think who faced this library understands.
The question arose whether there is some kind of library that could describe the entire future workbook, all its structure and contents, settings, formatting as a class, similar to django models, or a serializer rest, then you create a type of view to which you transfer data that will correspond to the described structure, you call the render_to_xls method, and voila, everything is assembled automatically?
I didn't find it myself. I'm thinking of writing something like this myself.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene Kuznetsov, 2018-10-26
@KEugene

Hmm ... something is not right.
Here is an example from https://xlsxwriter.readthedocs.io/

import xlsxwriter

# Create a workbook and add a worksheet.
workbook = xlsxwriter.Workbook('Expenses01.xlsx')
worksheet = workbook.add_worksheet()

# Some data we want to write to the worksheet.
expenses = (
    ['Rent', 1000],
    ['Gas',   100],
    ['Food',  300],
    ['Gym',    50],
)

# Start from the first cell. Rows and columns are zero indexed.
row = 0
col = 0

# Iterate over the data and write it out row by row.
for item, cost in (expenses):
    worksheet.write(row, col,     item)
    worksheet.write(row, col + 1, cost)
    row += 1

# Write a total using a formula.
worksheet.write(row, 0, 'Total')
worksheet.write(row, 1, '=SUM(B1:B4)')

workbook.close()

Created a book.
Added a sheet. This sheet is a set of cells.
Everything, further, is practically the same as in VBA. You can "cell by cell" set values. You can try directly with an array (there is also an example with a panda).
import pandas as pd

# Create a Pandas dataframe from the data.
df = pd.DataFrame({'Data': [10, 20, 30, 20, 15, 30, 45]})

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')

# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1')

# Close the Pandas Excel writer and output the Excel file.
writer.save()

Where is it even easier?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question