Skip to main content

Preserve good formatting when exporting to *.xlsx, *.yaml and *.json and when updating data.

Project description

pyexcel-export - Keep optimal formatting when exported to xlsx format

pyexcel-export is a wrapper around pyexcel, pyexcel-xlsx and openpyxl to read the formatting (stylesheets) and update the pre-existing file without destroying the stylesheets.

pyexcel-export also introduces two new exporting format, *.yaml and *.pyexcel.json which is based on NoIndentEncoder. This allows you to edit the spreadsheet in you favorite text editor, without being frustrated by automatically collapsed cells in Excel.

Known constraints

The "stylesheets" exported from Excel is in a very long base64 encoded format when exported to *.json or *.pyexcel.json, so exporting such to *.json is disabled by default.

As stylesheets copying works by openpyxl.worksheet.copier.WorksheetCopy, it may work only on values, styles, dimensions and merged cells, but charts might not be supported.

Installation

You can install pyexcel-export via pip:

$ pip install pyexcel-export

or clone it and install it.

Usage

Read *.pyexcel.json

>>> from pyexcel_export import get_data
>>> from collections import OrderedDict
>>> data, meta = get_data('test.yaml')  # *.pyexcel.json is also possible.
>>> data
OrderedDict([('test', [['id', 'English', 'Pinyin', 'Hanzi', 'Audio', 'Tags'], [1419644212689, 'Hello!', 'Nǐ hǎo!', '你好!', '[sound:tmp1cctcn.mp3]', ''], [1419644212690, 'What are you saying?', 'Nǐ shuō shénme?', '你说什么?', '[sound:tmp4tzxbu.mp3]', ''], [1419644212691, 'What did you do?', 'nǐ zuò le shénme ?', '你做了什么?', '[sound:333012.mp3]', '']])])
>>> meta
Meta([
  created : '2018-07-15T18:26:05.879991',
  has_header : True,
  freeze_header : True,
  col_width_fit_param_keys : True,
  col_width_fit_ids : True,
  bool_as_string : True,
  allow_table_hiding : True,
  _styles : <_io.BytesIO object at 0x10553b678>
])
>>> meta.matrix
[('created', '2018-07-15T05:32:43.976194'), ('modified', '2018-07-15T05:32:52.248192'), ('has_header', True), ('freeze_header', True), ('col_width_fit_param_keys', True), ('col_width_fit_ids', True), ('allow_hidden_tables', True), ('_styles', {"<class '_io.BytesIO'>": <_io.BytesIO object at 0x1103d9048>})]
>>> meta.excel_matrix
[('created', '2018-07-15T05:32:43.976194'), ('modified', '2018-07-15T05:32:52.248192'), ('has_header', {"<class 'bool'>": True}), ('freeze_header', {"<class 'bool'>": True}), ('col_width_fit_param_keys', {"<class 'bool'>": True}), ('col_width_fit_ids', {"<class 'bool'>": True}), ('allow_hidden_tables', {"<class 'bool'>": True}), ('_styles', {"<class '_io.BytesIO'>": <_io.BytesIO object at 0x10b56b048>})]

Exporting stylesheets

>>> from pyexcel_export import get_meta
>>> get_meta()
Meta([
  created : '2018-07-15T18:26:05.879991',
  has_header : True,
  freeze_header : True,
  col_width_fit_param_keys : True,
  col_width_fit_ids : True,
  bool_as_string : True,
  allow_table_hiding : True
])
>>> get_meta('test.xlsx')
Meta([
  created : '2018-07-15T18:26:05.879991',
  has_header : True,
  freeze_header : True,
  col_width_fit_param_keys : True,
  col_width_fit_ids : True,
  bool_as_string : True,
  allow_table_hiding : True,
  _styles : <_io.BytesIO object at 0x10553b678>
])

Saving files, while preserving the formatting.

>>> from pyexcel_export import get_meta, save_data
>>> from collections import OrderedDict
>>> meta = get_meta('test.xlsx')
>>> data = OrderedDict()
>>> data.update({"Sheet 1": [["header A", "header B", "header C"], [1, 2, 3]]})
>>> save_data("your_file.xlsx", data, meta=meta)

*.yaml format

!!python/object/apply:collections.OrderedDict
- - - _meta
    - - !!python/tuple [created, '2018-07-15T05:32:43.976194']
      - !!python/tuple [modified, '2018-07-15T13:31:47.725480']
      - !!python/tuple [has_header, true]
      - !!python/tuple [freeze_header, true]
      - !!python/tuple [col_width_fit_param_keys, true]
      - !!python/tuple [col_width_fit_ids, true]
      - !!python/tuple [allow_hidden_tables, true]
  - - test
    - - [id, English, Pinyin, Hanzi, Audio, Tags]
      - [1419644212689, Hello!, Nǐ hǎo!, 你好!, '[sound:tmp1cctcn.mp3]', '']
      - [1419644212690, 'What are you saying?', 'Nǐ shuō shénme?', 你说什么?, '[sound:tmp4tzxbu.mp3]',
        '']
      - [1419644212691, 'What did you do?', 'nǐ zuò le shénme ?', 你做了什么?, '[sound:333012.mp3]',
        '']

*.pyexcel.json format

{
  "_meta": [
    ["\"created\"", "\"2018-07-12T15:21:25.777499\""],
    ["\"modified\"", "\"2018-07-15T06:59:22.707162\""],
    ["\"has_header\"", "true"],
    ["\"freeze_header\"", "true"],
    ["\"col_width_fit_param_keys\"", "true"],
    ["\"col_width_fit_ids\"", "true"]
  ],
  "test": [
    ["\"id\"", "\"English\"", "\"Pinyin\"", "\"Hanzi\"", "\"Audio\"", "\"Tags\""],
    ["1419644212689", "\"Hello!\"", "\"Nǐ hǎo!\"", "\"你好!\"", "\"[sound:tmp1cctcn.mp3]\"", "\"\""],
    ["1419644212690", "\"What are you saying?\"", "\"Nǐ shuō shénme?\"", "\"你说什么?\"", "\"[sound:tmp4tzxbu.mp3]\"", "\"\""],
    ["1419644212691", "\"What did you do?\"", "\"nǐ zuò le shénme ?\"", "\"你做了什么?\"", "\"[sound:333012.mp3]\"", "\"\""]
  ]
}

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

pyexcel-export-0.2.4.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

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

pyexcel_export-0.2.4-py3-none-any.whl (24.6 kB view details)

Uploaded Python 3

File details

Details for the file pyexcel-export-0.2.4.tar.gz.

File metadata

File hashes

Hashes for pyexcel-export-0.2.4.tar.gz
Algorithm Hash digest
SHA256 1826f88f958d7dd3e3c73c49d31ecaf09faeaa243f548f73e6126649d17ba9ef
MD5 5c741ab2f46264f6a6b947ae73d0395c
BLAKE2b-256 d2cae4b81c3b2e4913db4d4447322fb59ee746418952f0afd0cdeb65c52ab238

See more details on using hashes here.

File details

Details for the file pyexcel_export-0.2.4-py3-none-any.whl.

File metadata

File hashes

Hashes for pyexcel_export-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e38caa0c03c233f775162160b84043d0f0b59ebf2d326b545abdd22dc8c1bbef
MD5 51871d072c46eea2249b09684f68cdf7
BLAKE2b-256 0d6fe5588c47ca21c5a27e9805fb6ced1889d3e4b9e0ee95fda6fb05b18e80cd

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