Skip to main content

Builton top of xlrd, package provides set of cell parsers that make your import easier.

Project description

Installation

Package installed as usually, with command

pip install xlsimport

package requires xlrd and dateutils.

Usage

Xlsimport provide only API for now, no demo nor default setup. But it’s simple to start using it.

First, you need to create descendant of xlsimport.models.Format class, for example:

class SubjectFormat(Format):
    cells = (
        {'name': 'Name', 'parsers': (TextCellToStringParser,)},
        {'name': 'Hours', 'parsers': (TextCellToIntParser, NumberCellToIntParser,)},
        {'name': 'Short name', 'parsers': (TextCellToStringParser,)},
    )

    def to_python(self, data_row):
        return {
            'name': data_row[0],
            'short_name': re.sub(r'[0-9-]+', '', data_row[2]),
            'hours': data_row[1],
        }

In this code Name column may contains only text cells and they are represented as strings. Second column Hours may contains text cells or number cells. They are represented as integers. Note that parsers applied with respect to order.

If you want to skip column, give parsers the value xlsimport.models.dummy_parsers. If cell must be empty there is blank_parsers value for that case.

Next you should make class that knows how to handle data (craate or update records). You also might want to place it into class like Django command.

Here’s my example:

...

class Command(BaseCommand):

    ...

    def handle(self, *args, **options):
        source_filename = args[0]
        source_file = open(source_filename, 'r')
        descriptor, name = tempfile.mkstemp()
        os.fdopen(descriptor, 'wb').write(source_file.read())
        doc = xlrd.open_workbook(name, formatting_info=True)
        format_doc = SubjectFormat(doc)

        for index, parsed_row in enumerate(format_doc):
            process_row(index, parsed_row)

process_row here is a function that takes dictionary from to_python method above.

Tip. I created a gist with nice example of linux dialog usage.

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xlsimport-0.1.tar.gz (4.6 kB view details)

Uploaded Source

File details

Details for the file xlsimport-0.1.tar.gz.

File metadata

  • Download URL: xlsimport-0.1.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for xlsimport-0.1.tar.gz
Algorithm Hash digest
SHA256 b61afc42f31b0c667f640fff92dcbd7119af2e255bfe3fa0a4e2f1085da1be5f
MD5 44e893ac6f9f3698cf975cd67fa67f67
BLAKE2b-256 4a08a1733df16b3ecae690a72eae02d4cb0fb31a6c14fbabb373861b8a1a0e48

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 Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page