Skip to main content

Read a CSV file and write an XLSX file with optional formatting.

Project description

dhk.csv2xlsx

GitHub

Latest Version Python Versions

Downloads Per Day Downloads Per Week Downloads Per Month

Introduction

dhk.csv2xlsx is a Python command-line tool for reading a CSV file and writing an XLSX file with optional formatting. It leverages the Python standard library csv module and the XlsxWriter package.

Simple Installation

A pedestrian command for installing the package is given below. Alternatively, for a more rewarding installation exercise, see section Recommended Installation.

pip install dhk.csv2xlsx

Usage

$ csv2xlsx --help
usage: csv2xlsx [-h] [--force] [--output OUTPUT_FILE]
                [--settings-file SETTINGS_FILE] [--verbose]
                [--generate-settings-file | --transform-csv]
                [CSV_FILE]

Read a CSV file and write an XLSX file.

The program exposes much of the formatting functionality of the XlsxWriter
packages Workbook class through the optional SETTINGS_FILE.  For details about
the Workbook class, see https://xlsxwriter.readthedocs.io/workbook.html.  For
details about the Worksheet class, see
https://xlsxwriter.readthedocs.io/worksheet.html.  For details about the Format
class, see https://xlsxwriter.readthedocs.io/format.html.

positional arguments:
  CSV_FILE              CSV file

options:
  -h, --help            show this help message and exit
  --force, -f           force; suppress prompts
  --output, -o OUTPUT_FILE
                        output file; default: CSV_FILE - .csv + .xlsx
  --settings-file, -s SETTINGS_FILE
                        settings file
  --verbose, -v         verbose
  --generate-settings-file, -g
                        generate settings file; file defaults to
                        sample.settings.json
  --transform-csv, -t   transform CSV to XLSX; default True

examples:

    csv2xlsx \
        --generate-settings-file

    csv2xlsx \
        CSV_FILE

    csv2xlsx \
        --settings-file SETTINGS_FILE \
        CSV_FILE

    csv2xlsx \
        --settings-file SETTINGS_FILE \
        --output OUTPUT \
        CSV_FILE

execution details:

The Workbook class instance attribute `options` is set to `{'constant_memory':
True}` merged with the SETTINGS_FILE `workbook_options` dictionary.

The Workbook class instance attribute `formats[0]` stores a default Format
object.  The SETTINGS_FILE `workbook_format` dictionary can be used to set the
attributes of this default Format object.  The available dictionary keywords
correspond to the list of properties in the table at
https://xlsxwriter.readthedocs.io/format.html#format-methods-and-format-properties.

The Workbook class instance method `add_format()` is used to add Format class
instances to the Workbook class instance.  The program iterates over each of
the SETTINGS_FILE `cell_format_settings` dictionary key-value pairs.  The
SETTINGS_FILE `workbook_format` dictionary is merged with the value of the
`cell_format_settings` key-value pair, and this merged value is the argument to
`add_format()`.  The key-value pair formed by the key from the
`cell_format_settings` key-value pair and the `add_format()` return value is
stored in a `cell_formats` dictionary for later use when setting Worksheet
class instance columns and rows.

The Worksheet class instance method `set_column()` is used to set the width,
cell format, and options for specified columns.  The program iterates over each
of the SETTINGS_FILE `column_settings` list elements.  For each element, the
`first_col` and `last_col` arguments to `set_column()` can be specified in
absolute terms via a `columns` key-value pair with the string value specified
by ordinal number (`'1'`, `'1:2'`, etc.) or by `A1` style notation (`'A'`,
`'A:B'`).  Alternatively, if the `columns` key-value pair is not present, then
`first_col` is simply the next column, and `last_col` is either the same as
`first_col` or is set based on a `span` key-value pair.  The `width` argument
to `set_column()` is set to the value of the `width` key-value pair, defaulting
to 10 if not specified.  The `cell_format` argument to `set_column()` is set
based on the value of the `cell_format` key-value pair.  This value must
correspond to the key of one of the SETTINGS_FILE `cell_format_settings`
key-value pairs.  If a `cell_format` key-value pair is not provided, then the
argument defaults to the Workbook class instance attribute `formats[0]`.  The
`options` argument to `set_column()` is set to the value of the `options`
key-pair.  The value of the `data_type` key-pair is stored for each column for
later use when parsing CSV data.

The Worksheet class instance method `set_row()` is used to set the cell format
for specified rows.  The program iterates over each of the SETTINGS_FILE
`row_settings` list elements.  For each element, `first_row` and `last_row` can
be specified in absolute terms via a `rows` key-value pair with the string
value specified by ordinal number (`'1'`, `'1:2'`, etc.).  Alternatively, if
the `rows` key-value pair is not present, then `first_row` is simply the next
column, and `last_row` is either the same as `first_row` or is set based on a
`span` key-value pair.  For each `row` in the sequence determined by
`first_row` and `last_row`, `set_row()` is called with `row` as the first
positional argument and the argument `cell_format` set based on the value of
the `cell_format` key-value pair.  This value must correspond to the key of one
of the SETTINGS_FILE `cell_format_settings` key-value pairs.  If a
`cell_format` key-value pair is not provided, then the argument defaults to
the Workbook class instance attribute `formats[0]`.  The value of the
`data_type` key-pair is stored for each row for later use when parsing CSV
data.

Each row of the CSV file is read.  The value of each column of the row is
handled.  If there was a `data_type` specified in the SETTINGS_FILE for the
row, then that is used.  Otherwise, an attempt is made to get a `data_type`
that was specified in the SETTINGS_FILE for the column.  Valid values for
`data_type` are: `decimal`, `bool`, `date`, `datetime`, `formula`, and
`string`.  If `data_type` was not specified or an exception occurs when
attempting to parse the CSV value in terms of the given `data_type`, then the
CSV value is written as a `string`.

The Worksheet class instance method `autofilter` is called with arguments based
on the `autofilter` object in the SETTINGS_FILE `workbook_settings` dictionary.
Both row-column notation (`(first_row, first_col, last_row, last_col)`) and
`A1` style notation (`'A1:D11'`) are supported.  If row-column notation is
used, then a negative number is replaced with the maximum row or column
available in the data.

The Worksheet class instance method `freeze_panes` is called with arguments
based on the `freeze_panes` object in the SETTINGS_FILE `workbook_settings`
dictionary.  Both row-column notation (`(row, col[, top_row, left_col])`) and
`A1` style notation (`'A2'`) are supported.

Recommended Installation

pyenv is a tool for installing multiple Python environments and controlling which one is in effect in the current shell.

pipx is a tool for installing and running Python applications in isolated environments.

Assuming these have been installed correctly...

Install Python Under pyenv

The version of Python under which this package was last developed and tested is stored in .python-version.

To capture this Python version to a shell variable, execute the commands below. PYTHON_VERSION should be set to something like 3.13.3.

PYTHON_VERSION="$(
    wget \
        -O - \
        https://raw.githubusercontent.com/DavidKiesel/python-csv2xlsx/refs/heads/main/.python-version
)"

echo "$PYTHON_VERSION"

To determine if the .python-version version of Python has already been installed under pyenv, execute the command below. If it has not been installed, then a warning message will be displayed.

PYENV_VERSION="$PYTHON_VERSION" \
python --version

If it has already been installed, then proceed to section Install Package Using pipx.

Otherwise, to install the given version of Python under pyenv, execute the command below.

pyenv install "$PYTHON_VERSION"

If the install was successful, then proceed to section Install Package Using pipx.

If instead there is a warning that the definition was not found, then you will need to upgrade pyenv.

If pyenv was installed through a package manager, then consider upgrading it through that package manager. For example, if pyenv was installed through brew, then execute the commands below.

brew update

brew upgrade pyenv

Alternatively, you could attempt to upgrade pyenv through the command below.

pyenv update

Once pyenv has been upgraded, to install the given version of Python under pyenv, execute the command below.

pyenv install "$PYTHON_VERSION"

Install Package Using pipx

Only proceed from here if the instructions in section Install Python Under pyenv have been completed successfully.

At this point, shell variable PYTHON_VERSION should already contain the appropriate Python version. If not, execute the commands below.

PYTHON_VERSION="$(
    wget \
        -O - \
        https://raw.githubusercontent.com/DavidKiesel/python-csv2xlsx/refs/heads/main/.python-version
)"

echo "$PYTHON_VERSION"

To install the package hosted at PyPI using pipx, execute the command below.

pipx \
    install \
    --python "$(PYENV_VERSION="$PYTHON_VERSION" pyenv which python3)" \
    dhk.csv2xlsx

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

dhk_csv2xlsx-2.0.0.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dhk_csv2xlsx-2.0.0-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file dhk_csv2xlsx-2.0.0.tar.gz.

File metadata

  • Download URL: dhk_csv2xlsx-2.0.0.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for dhk_csv2xlsx-2.0.0.tar.gz
Algorithm Hash digest
SHA256 e8203af9b4308badae84621abac06287bfb49eb8a184698f9531b8e3d38a01c8
MD5 bb0265d344be8b5d9fe4670dce539e15
BLAKE2b-256 fe012a72fad0356907e2ae9dea20c8438268f718da6aec77dbae3dbb82da9576

See more details on using hashes here.

File details

Details for the file dhk_csv2xlsx-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: dhk_csv2xlsx-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for dhk_csv2xlsx-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f73b8c1583108bb666f5054892d0d9040d59d90fe6e8f8552cb35395c2695eea
MD5 64ac881b435624cfa75772c460bdc426
BLAKE2b-256 a422b86eacfea38ba41679b0d2317c8d5bc9128d64ce8b32c1592da8932ebddb

See more details on using hashes here.

Supported by

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