Skip to main content

A Python library for writing .xlsx files.

Project description

https://img.shields.io/pypi/v/xlsxcessive.svg https://img.shields.io/pypi/pyversions/xlsxcessive.svg tests Code style: Black https://img.shields.io/badge/skeleton-2022-informational

XlsXcessive provides a Python API for writing Excel/OOXML compatible .xlsx spreadsheets. It generates the XML and uses openpack to wrap it up into an OOXML compatible ZIP file.

Creating a Workbook

The starting point for generating an .xlsx file is a workbook:

from xlsxcessive.workbook import Workbook

workbook = Workbook()

Adding Worksheets

The workbook alone isn’t very useful. Multiple worksheets can be added to the workbook and contain the cells with data, formulas, etc. Worksheets are created from the workbook and require a name:

sheet1 = workbook.new_sheet('Sheet 1')

Working With Cells

Add some cells to the worksheet:

sheet1.cell('A1', value='Hello, world')
sheet1.cell('B1', value=7)
sheet1.cell('C1', value=3.14)
sheet1.cell('D1', value=decimal.Decimal("19.99"))

Strings, integers, floats and decimals are supported.

Add cells via row index and column index:

sheet1.cell(coords=(0, 4), value="Added via row/col index")

This form of addressing is useful when iterating over data structures to populate a sheet with cells.

Calculations With Formulas

Cells can also contain formulas. Formulas are created with a string representing the formula code. You can optionally supply a precalcuated value and a shared boolean flag to share the formula across a number of cells. The first cell to reference a shared formula as its value is the master cell for the formula. Other cells may also reference the formula:

formula = sheet1.formula('B1 + C1', shared=True)
sheet1.cell('C2', formula) # master
sheet1.cell('D2', formula) # shared, references the master formula

Cells With Style

The library contains basic support for styling cells. The first thing to do is create a style format. Style formats are shared on a stylesheet on the workbook:

bigfont = workbook.stylesheet.new_format()
bigfont.font(size=24, bold=True)

Apply the format to cells:

sheet1.cell('A2', 'HI', format=bigfont)

Other supported style transformations include cell alignment and borders:

col_header = workbook.stylesheet.new_format()
col_header.align('center')
col_header.border(bottom='medium')

Adjusting Column Width

It is possible to adjust column widths on a sheet. The column width is specified by either number or index:

# these are the same column
sheet1.col(index=0, width=10)
sheet1.col(number=1, width=10)

TODO: Referencing columns by letters.

Merging Cells

Cells can be merged together. The left-most cell in the merge range should contain the data:

from xlsxcessive.worksheet import Cell
a3 = sheet1.cell('A3', 'This is a lot of text to fit in a tiny cell')
a3.merge(Cell('D3'))

Save Your Work

You can save the generated OOXML data to a local file or to an output file stream:

# local file
save(workbook, 'financials.xlsx')

# stream
save(workbook, 'financials.xlsx', stream=sys.stdout)

Future

This is certainly a work in progress. The focus is going to be on improving the features that can be written out in the .xlsx file. That means more data types, styles, metadata, etc. I also want to improve the validation of data before it is written in an incorrect manner and Excel complains about it. I don’t think this library will ever be crafted to read .xlsx files. That’s a job for another library that can hate its life.

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

xlsxcessive-1.1.3.tar.gz (36.5 kB view details)

Uploaded Source

Built Distribution

xlsxcessive-1.1.3-py3-none-any.whl (26.4 kB view details)

Uploaded Python 3

File details

Details for the file xlsxcessive-1.1.3.tar.gz.

File metadata

  • Download URL: xlsxcessive-1.1.3.tar.gz
  • Upload date:
  • Size: 36.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.0

File hashes

Hashes for xlsxcessive-1.1.3.tar.gz
Algorithm Hash digest
SHA256 c56defa7213b9ddc997b879ecc78dd920a43c0717f9bf31849eccf472035f2bd
MD5 00aa91ea4e0714f5a67e6677295773d1
BLAKE2b-256 13903f45317a6b56f4581af1f4564231684b0768eb222dff923a91eb2c022ff1

See more details on using hashes here.

File details

Details for the file xlsxcessive-1.1.3-py3-none-any.whl.

File metadata

  • Download URL: xlsxcessive-1.1.3-py3-none-any.whl
  • Upload date:
  • Size: 26.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.0

File hashes

Hashes for xlsxcessive-1.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 eef01a09ab9a8dc9d0316929b6cd1c7ad1410dad1177f3dde2e3475065112c45
MD5 ef1617c23400d72a0c614c3a0bea5eaa
BLAKE2b-256 ee2011b08e6b4e08418796b7b2363aa3a40e4eabd99a19410b2e668ef0f5e04e

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