Read and write Apple Numbers spreadsheets
Project description
numbers-parser
numbers-parser
is a Python module for parsing Apple Numbers.numbers
files. It supports Numbers files generated by Numbers version 10.3, and up with the latest tested version being 13.2 (current as of September 2023).
It supports and is tested against Python versions from 3.8 onwards. It is not compatible with earlier versions of Python.
Installation
python3 -m pip install numbers-parser
A pre-requisite for this package is python-snappy which will be installed by Python automatically, but python-snappy also requires that the binary libraries for snappy compression are present.
The most straightforward way to install the binary dependencies is to use Homebrew and source Python from Homebrew rather than from macOS as described in the python-snappy github:
For Intel Macs:
brew install snappy python3
CPPFLAGS="-I/usr/local/include -L/usr/local/lib" python3 -m pip install python-snappy
For Apple Silicon Macs:
brew install snappy python3
CPPFLAGS="-I/opt/homebrew/include -L/opt/homebrew/lib" python3 -m pip install python-snappy
For Linux (your package manager may be different):
sudo apt-get -y install libsnappy-dev
On Windows, you will need to either arrange for snappy to be found for VSC++ or you can install python binary libraries compiled by Christoph Gohlke. You must select the correct python version for your installation. For example for python 3.11:
C:\Users\Jon>pip install C:\Users\Jon\Downloads\python_snappy-0.6.1-cp311-cp311-win_amd64.whl
API changes in version 4.0
To better partition cell styles, background image data which was supported in earlier versions through the methods image_data
and image_filename
is now part of the new cell_style
property. Using the deprecated methods image_data
and image_filename
will issue a DeprecationWarning
if used.The legacy methods will be removed in a future version of numbers-parser.
NumberCell
cell values are now limited to 15 significant figures to match the implementation of floating point numbers in Apple Numbers. For example, the value 1234567890123456
is rounded to 1234567890123460
in the same way as in Numbers. Previously, using native float
with no checking resulted in rounding errors in unpacking internal numbers. Attempting to write a number with too many significant digits results in a RuntimeWarning
.
The previously deprecated methods Document.sheets()
and Sheet.tables()
are now only available using the properties of the same name (see examples in this README).
Usage
Reading documents:
from numbers_parser import Document
doc = Document("my-spreadsheet.numbers")
sheets = doc.sheets
tables = sheets[0].tables
rows = tables[0].rows()
Referring to sheets and tables
Sheets and tables are iterables that can be indexed using either an integer index or using the name of the sheet/table:
# list access method
sheet_1 = doc.sheets[0]
print("Opened sheet", sheet_1.name)
# dict access method
table_1 = sheets["Table 1"]
print("Opened table", table_1.name)
Accessing data
Table
objects have a rows
method which contains a nested list with an entry for each row of the table. Each row is itself a list of the column values.
data = sheets["Table 1"].rows()
print("Cell A1 contains", data[0][0])
print("Cell C2 contains", data[2][1])
Cells are objects with a common base class of Cell
. All cell types have a property value
which returns the contents of the cell in as a python datatype. numbers-parser
uses pendulum instead of python's builtin types. Available cell types are:
Cell type | value type | Additional properties |
---|---|---|
NumberCell | float |
|
TextCell | str |
|
RichTextCell | str |
See Bullets and lists |
EmptyCell | None |
|
BoolCell | bool |
|
DateCell | pendulum.datetime |
|
DurationCell | pendulum.duration |
|
ErrorCell | None |
|
MergedCell | None |
See Merged cells |
Where cell values are not None
the property formatted_value
returns the cell value as a str
as displayed in Numbers. Cells that have no values in a table are represented as EmptyCell
and cells containing evaluation errors of any kind ErrorCell
.
Cell references
Data for single cells is accessed using Table.cell()
. Cell references can be either zero-offset row/column integers or an Excel/Numbers cell reference using a column letter and row number:
doc = Document("my-spreadsheet.numbers")
sheets = doc.sheets
tables = sheets["Sheet 1"].tables
table = tables["Table 1"]
# row, column syntax
print("Cell A1 contains", table.cell(0, 0))
# Excel/Numbers-style cell references
print("Cell C2 contains", table.cell("C2"))
Merged cells
Cell.is_merged
returns True
for any cell that is the result of merging rows and/or columns. Cells eliminated from the table by the merge can still be indexed using Table.cell()
and are of type MergedCell
.
Consider this example:
A1 | B1 |
A2 |
The properties of merges are tested using the following properties:
Cell | Type | value |
is_merged |
size |
rect |
merge_range |
---|---|---|---|---|---|---|
A1 | TextCell | A1 |
False |
(1, 1) | None |
None |
A2 | TextCell | A2 |
False |
(1, 1) | None |
None |
B1 | TextCell | B1 |
True |
(2, 1) | None |
None |
B2 | MergedCell | None |
False |
None |
(1, 0, 2, 0) | "B1:B2" |
The tuple values of the rect
property of a MergedCell
are also available using the properties row_start
, col_start
, row_end
, and col_end
.
Row and column iterators
Tables have iterators for row-wise and column-wise iteration with each iterator returning a list of the cells in that row or column
for row in table.iter_rows(min_row=2, max_row=7, values_only=True):
sum += row
for col in table.iter_cols(min_row=2, max_row=7):
sum += col.value
Formulas
Formula evaluation relies on Numbers storing current values which should usually be the case. In cells containing a formula, value
returns the computed value of the formula. The formula itself is available using the formula
property.
Pandas
Since the return value of rows()
is a list of lists, you can pass this directly to pandas. Assuming you have a Numbers table with a single header which contains the names of the pandas series you want to create you can construct a pandas dataframe using:
import pandas as pd
doc = Document("simple.numbers")
sheets = doc.sheets
tables = sheets[0].tables
data = tables[0].rows(values_only=True)
df = pd.DataFrame(data[1:], columns=data[0])
Bullets and lists
Cells that contain bulleted or numbered lists can be identified by the is_bulleted
property. Data from such cells is returned using the value
property as with other cells, but can additionally extracted using the bullets
property. bullets
returns a list of the paragraphs in the cell without the bullet or numbering character. Newlines are not included when bullet lists are extracted using bullets
.
doc = Document("bullets.numbers")
sheets = doc.sheets
tables = sheets[0].tables
table = tables[0]
if not table.cell(0, 1).is_bulleted:
print(table.cell(0, 1).value)
else:
bullets = ["* " + s for s in table.cell(0, 1).bullets]
print("\n".join(bullets))
Bulleted and numbered data can also be extracted with the bullet or number characters present in the text for each line in the cell in the same way as above but using the formatted_bullets
property. A single space is inserted between the bullet character and the text string and in the case of bullets, this will be the Unicode character seen in Numbers, for example "• some text"
.
Hyperlinks
Numbers does not support hyperlinks to cells within a spreadsheet, but does allow embedding links in cells. When cells contain hyperlinks, numbers_parser
returns the text version of the cell. The hyperlinks
property of cells where is_bulleted
is True
is a list of text and URL tuples:
cell = table.cell(0, 0)
(text, url) = cell.hyperlinks[0]
Styles
numbers_parser
currently only supports paragraph styles and cell styles. The following paragraph styles are supported:
- font attributes: bold, italic, underline, strikethrough
- font selection and size
- text foreground color
- horizontal and vertical alignment
- cell background color
- cell indents (first line, left, right, and text inset)
Table styles that allow new tables to adopt a style across the whole table are not planned.
Numbers conflates style attributes that can be stored in paragraph styles (the style menu in the text panel) with the settings that are available on the Style tab of the Text panel. Some attributes in Numbers are not applied to new cells when a style is applied. To keep the API simple, numbers-parser
packs all styling into a single Style
object. When a document is saved, the attributes not stored in a paragraph style are applied to each cell that includes it. Attributes behaving in this way are currently Cell.alignment.vertical
and Cell.style.text_inset
. The cell background color Cell.style.bg_color
also behaves this way, though this is in line with the separation in Numbers.
Reading styles
The cell method style
returns a Style
object containing all the style information for that cell. Cells with identical style settings contain references to a single style object.
Cell style attributes can be returned using a number of methods:
Cell.style.alignment
: the horizontal and vertical alignment of the cell as anAlignment
names tupleCell.style.bg_color
: cell background color as anRGB
named tuple, or a list ofRGB
values for gradientsCell.style.bold
:True
if the cell font is boldCell.style.font_color
: font color as anRGB
named tupleCell.style.font_size
: font size in points (float
)Cell.style.font_name
: font name (str
)Cell.style.italic
:True
if the cell font is italicCell.style.name
: cell style (str
)Cell.style.underline
:True
if the cell font is underlineCell.style.strikethrough
:True
if the cell font is strikethroughCell.style.first_indent
: first line indent in points (float
)Cell.style.left_indent
: left indent in points (float
)Cell.style.right_indent
: right indent in points (float
)Cell.style.text_inset
: text inset in points (float
)Cell.style.text_wrap
:True
if text wrapping is enabled (default for new cells)
Cell images
The methods style.bg_image.filename
and style.bg_image.data
return data about the image used for a cell's background, where set. If a cell has no background image, style.bg_image
is None
.
cell = table.cell("B1")
with open (cell.style.bg_image.filename, "wb") as f:
f.write(cell.style.bg_image.data)
Due to a limitation in Python's ZipFile, Python versions older than 3.11 do not support image filenames with UTF-8 characters (see issue 69). cell.style.bg_image
returns None
for such files and issues a RuntimeWarning
.
Formatting
In addition to rendering values as they are displayed in Numbers using the cell property formatted_value
, numbers-parser
has limited support for setting cell formats when saving files.
Formats are provided to the Table.write
method:
date = datetime(2023, 4, 1, 13, 25, 42)
table.write(0, 0, date, formatting={"date_time_format": "EEEE, d MMMM yyyy"})
table.write(0, 1, 1234.560, formatting={"decimal_places": 3})
The following cell types are supported along with the associated formatting parameters:
Cell Type | formatting parameter |
Description |
---|---|---|
DateCell |
date_time_format
| A POSIX strftime -like formatting string. See Date/time formatting for a list of supported directives |
NumberCell |
decimal_places |
The number of decimal places, or None for automatic |
negative_style
| How negative numbers are represented | |
show_thousands_separator
| True if the number should include a thousands seperator, e.g. , |
Date/time formatting
date_time_format
uses Numbers notation for date and time formatting rather than POSIX strftime
as there are a number of extensions. Date components are specified using directives which must be separated by whitespace. Supported directives are:
Directive | Meaning | Example |
---|---|---|
a | Locale’s AM or PM | am, pm |
EEEE | Full weekday name | Monday, Tuesday, ... |
EEE | Abbreviated weekday name | Mon, Tue, ... |
yyyy | Year with century as a decimal number | 1999, 2023, etc. |
yy | Year without century as a zero-padded decimal number | 00, 01, ... 99 |
y | Year without century as a decimal number | 0, 1, ... 99 |
MMMM | Full month name | January, February, ... |
MMM | Abbreviated month name | Jan, Feb, ... |
MM | Month as a zero-padded decimal number | 01, 02, ... 12 |
M | Month as a decimal number | 1, 2, ... 12 |
d | Day as a decimal number | 1, 2, ... 31 |
dd | Day as a zero-padded decimal number | 01, 02, ... 31 |
DDD | Day of the year as a zero-padded 3-digit number | 001 - 366 |
DD | Day of the year as a minimum zero-padded 2-digit number | 01 - 366 |
D | Day of the year | 1 - 366 |
HH | Hour (24-hour clock) as a zero-padded decimal number | 00, 01, ... 23 |
H | Hour (24-hour clock) as a decimal number | 0, 1, ... 23 |
hh | Hour (12-hour clock) as a zero-padded decimal number | 01, 02, ... 12 |
h | Hour (12-hour clock) as a decimal number | 1, 2, ... 12 |
k | Hour (24-hour clock) as a decimal number to 24 | 1, 2, ... 24 |
kk | Hour (24-hour clock) as a zero-padded decimal number to 24 | 01, 02, ... 24 |
K | Hour (12-hour clock) as a decimal number from 0 | 0, 1, ... 11 |
KK | Hour (12-hour clock) as a zero-padded decimal number from 0 | 00, 01, ... 11 |
mm | Minutes as a zero-padded number | 00, 01, ... 59 |
m | Minutes as a number | 0, 1, ... 59 |
ss | Seconds as a zero-padded number | 00, 01, ... 59 |
s | Seconds as a number | 0, 1, ... 59 |
W | Week number in the month (first week is zero) | 0, 1, ... 5 |
ww | Week number of the year (Monday as the first day of the week) | 0, 1, ... 53 |
G | AD or BC (only AD is supported) | AD |
F | How many times the day of falls in the month | 1, 2, ... 5 |
S | Seconds to one decimal place | 0 - 9 |
SS | Seconds to two decimal places | 00 - 99 |
SSS | Seconds to three decimal places | 000 - 999 |
SSSS | Seconds to four decimal places | 0000 - 9999 |
SSSSS | Seconds to five decimal places | 00000 - 9999 |
Number formatting
All formatting
parameters for NumberCell
cells are optional and formatting defaults to automatic number of decimals, standard negative numbr notation and no thousands separator.
The negative_style
must be a valid constants.NegativeNumberStyle
enum. Supported values are:
Value | Examples |
---|---|
MINUS |
-1234.560 |
RED |
1234.560 |
PARENTHESES |
(1234.560) |
RED_AND_PARENTHESES |
(1234.560) |
Borders
numbers-parser
supports reading and writing cell borders, though the interface for each differs. Individual cells can have each of their four borders tested, but when drawing new borders, these are set for the table to allow for drawing borders across multiple cells. Setting the border of merged cells is not possible unless the edge of the cells is at the end of the merged region.
Borders are represented using the Border
class that can be initialized with line width, color and line style:
border = Border(4.0, RGB(0, 162, 255), "solid"))
Valid values for the line style
parameter are "solid"
, "dashes"
, "dots"
and "none"
.
Reading Cell Borders
Cells have a property border
which itself has the properties top
, right
, bottom
and left
, each of which is a Border
class representing the line type for that cell. Cells with no border set at all, and merged cells which are inside the range of the merge return None
for these cells. The absence of a specified border is different from no border in Numbers which is a valid Border
class with style="none"
.
Writing Cell Borders
The Table
method set_cell_border()
sets the border for a cell edge or a range of cells:
table.set_cell_border("C1", ["top", "left"], Border(0.0, RGB(0, 0, 0), "none"))
table.set_cell_border(0, 4, "right", Border(1.0, RGB(0, 0, 0), "solid"), 3)
The last positional parameter specifies the length of the border and defaults to 1. A single call to set_cell_border()
can set the borders to one or more sides of the cell as above. Like Table.write()
, set_cell_border()
supports both row/column and Excel-style cell references.
Writing Numbers files
Whilst support for writing numbers files has been stable since version 3.4.0, you are highly recommended not to overwrite working Numbers files and instead save data to a new file.
Limitations
Current limitations to write support are:
- Creating cells of type
BulletedTextCell
is not supported - Formats cannot be defined for
DurationCell
orDateCell
- New tables are inserted with a fixed offset below the last table in a worksheet which does not take into account title or caption size
- New sheets insert tables with formats copied from the first table in the previous sheet rather than default table formats
Cell values
numbers-parser
will automatically empty rows and columns for any cell references that are out of range of the current table. The write
method accepts the same cell numbering notation as cell
plus an additional argument representing the new cell value. The type of the new value will be used to determine the cell type.
doc = Document("old-sheet.numbers")
sheets = doc.sheets
tables = sheets[0].tables
table = tables[0]
table.write(1, 1, "This is new text")
table.write("B7", datetime(2020, 12, 25))
doc.save("new-sheet.numbers")
Sheet names and table names can be changed by assigning a new value to the name
of each:
sheets[0].name = "My new sheet"
tables[0].name = "Edited table"
Adding tables and sheets
Additional tables and worksheets can be added to a Document
before saving. If no sheet name or table name is supplied, numbers-parser
will use Sheet 1
, Sheet 2
, etc.
doc = Document()
doc.add_sheet("New Sheet", "New Table")
sheet = doc.sheets["New Sheet"]
table = sheet.tables["New Table"]
table.write(1, 1, 1000)
table.write(1, 2, 2000)
table.write(1, 3, 3000)
doc.save("sheet.numbers")
Table geometries
numbers-parser
can query and change the position and size of tables. Changes made to a table's row height or column width is retained when files are saved.
Row and column sizes
Row heights and column widths are queried and set using the row_height
and col_width
methods:
doc = Document("sheet.numbers")
table = doc.sheets[0].tables[0]
print(f"Table size is {table.height} x {table.width}")
print(f"Table row 1 height is {table.row_height(0)}")
table.row_height(0, 40)
print(f"Table row 1 height is now {table.row_height(0)}")
print(f"Table column A width is {table.col_width(0)}")
table.col_width(0, 200)
print(f"Table column A width is {table.col_width(0)}")
Header row and columns
When new tables are created, numbers-parser
follows the Numbers convention of creating a table with one row header and one column header. You can change the number of headers by modifying the appropriate property:
doc = Document("sheet.numbers")
table = doc.sheets[0].tables[0]
table.num_header_rows = 2
table.num_header_cols = 0
doc.save("saved.numbers")
A zero header count will remove the headers from the table. Attempting to set a negative number of headers, or using more headers that rows or columns in the table will raise a ValueError
exception.
Positioning tables
By default, new tables are positioned at a fixed offset below the last table vertically in a sheet and on the left side of the sheet. Large table headers and captions may result in new tables overlapping existing ones. The add_table
method takes optional coordinates for positioning a table. A table's height and coordinates can also be queried to help aligning new tables:
(x, y) = sheet.table[0].coordinates
y += sheet.table[0].height + 200.0
new_table = sheet.add_table("Offset Table", x, y)
Editing paragraph styles
Cell text styles, known as paragraph styles, are those applied by the Text tab in Numbers Format pane. To simplify the API, when writing documents, it is not possible to make ad hoc changes to cells without assigning an existing style or creating a new one. This differs to the Numbers interface where cells can have modified styles on a per cell basis. Such styles are read correctly when reading Numbers files.
Character styles, which allow formatting changes within cells such as "This is bold text" are not supported.
Styles are created using the Document
's add_style
method, and can be applied to cells either as part of a write
or using set_cell_style
:
red_text = doc.add_style(
name="Red Text",
font_name="Lucida Grande",
font_color=RGB(230, 25, 25),
font_size=14.0,
bold=True,
italic=True,
alignment=Alignment("right", "top"),
)
table.write("B2", "Red", style=red_text)
table.set_cell_style("C2", red_text)
New styles are automatically added to the list of styles selectable in the Numbers Text pane.
Cell styles can also be referred to by name in both Table.write
and Table.set_cell_style
. A dict
of available styles is returned by Document.styles
. This contains key value pairs of style names and Style
objects. Any changes to Style
objects in the document are written back such that those styles are changed for all cells that use them.
doc = Document("styles.numbers")
styles = doc.styles
styles["Title"].font_size = 20.0
Since Style
objects are shared, changing Cell.style.font_size
will have the effect of changing the font size for that style and will in turn affect the styles of all cells using that style.
Command-line scripts
When installed from PyPI, a command-like script cat-numbers
is installed in Python's scripts folder. This script dumps Numbers spreadsheets into Excel-compatible CSV format, iterating through all the spreadsheets passed on the command-line.
usage: cat-numbers [-h] [-T | -S | -b] [-V] [--debug] [--formulas]
[--formatting] [-s SHEET] [-t TABLE] [document ...]
Export data from Apple Numbers spreadsheet tables
positional arguments:
document Document(s) to export
optional arguments:
-h, --help show this help message and exit
-T, --list-tables List the names of tables and exit
-S, --list-sheets List the names of sheets and exit
-b, --brief Don't prefix data rows with name of sheet/table (default: false)
-V, --version
--debug Enable debug output
--formulas Dump formulas instead of formula results
--formatting Dump formatted cells (durations) as they appear in Numbers
-s SHEET, --sheet SHEET Names of sheet(s) to include in export
-t TABLE, --table TABLE Names of table(s) to include in export
Note: --formatting
will return different capitalization for 12-hour times due to differences between Numbers' representation of these dates and datetime.strftime
. Numbers in English locales displays 12-hour times with 'am' and 'pm', but datetime.strftime
on macOS at least cannot return lower-case versions of AM/PM.
Numbers File Formats
Numbers uses a proprietary, compressed binary format to store its tables. This format is comprised of a zip file containing images, as well as
Snappy-compressed Protobuf .iwa
files containing metadata, text, and all other definitions used in the spreadsheet.
Protobuf updates
As numbers-parser
includes private Protobuf definitions extracted from a copy of Numbers, new versions of Numbers will inevitably create .numbers
files that cannot be read by numbers-parser
. As new versions of Numbers are released, running make bootstrap
will perform all the steps necessary to recreate the protobuf files used numbers-parser
to read Numbers spreadsheets.
The default protobuf package installation may not include the C++ optimized version which is required by the bootstrapping scripts to extract protobufs. You will receive the following error during build if this is the case:
This script requires the Protobuf installation to use the C++ implementation. Please reinstall Protobuf with C++ support.
To include the C++ support, download a released version of Google protobuf from github. Build instructions are described in src/README.md
.These have changed greatly over time, but as of April 2023, this was useful:
bazel build :protoc :protobuf
cmake . -DCMAKE_CXX_STANDARD=14
cmake --build . --parallel 8
export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=cpp
export LD_LIBRARY_PATH=../bazel-bin/src/google
cd python
python3 setup.py -q bdist_wheel --cpp_implementation --warnings_as_errors --compile_static_extension
This can then be used make bootstrap
in the numbers-parser
source tree. The signing workflow assumes that you have an Apple Developer Account and that you have created provisioning profile that includes iCloud. Using a self-signed certificate does not seem to work, at least on Apple Silicon (a working PR contradicting this is greatly appreciated).
make bootstrap
requires PyObjC to generate font maps, but this dependency is excluded from Poetry to ensure that tests can run on non-Mac OSes. You can run poetry run pip install PyObjC
to get the required packages.
Credits
numbers-parser
was built by Jon Connell but relies heavily on from prior work by Peter Sobot to read the IWA format archives used by Apple's iWork family of applications, and to regenerate the mapping files required for Python. Both modules are derived from previous work by Sean Patrick O'Brien.
Decoding the data structures inside Numbers files was helped greatly by Stingray-Reader by Steven Lott.
Formula tests were adapted from JavaScript tests used in fast-formula-parser.
Decimal128 conversion to and from byte storage was adapted from work done by the SheetsJS project. SheetJS also helped greatly with some of the steps required to successfully save a Numbers spreadsheet.
License
All code in this repository is licensed under the MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file numbers_parser-4.6.0.tar.gz
.
File metadata
- Download URL: numbers_parser-4.6.0.tar.gz
- Upload date:
- Size: 274.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.4.2 CPython/3.11.3 Darwin/23.2.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fe2762bc435a7914ead8c0b82bd726b2ef3b079d025160187f86a995c481959c |
|
MD5 | 2dee29238e703d6ac83780973a046d98 |
|
BLAKE2b-256 | be9fb60c2abdc28df3cdab1ff65c1ac90ea7d70137a9aa78e90a32b15884605f |
File details
Details for the file numbers_parser-4.6.0-py3-none-any.whl
.
File metadata
- Download URL: numbers_parser-4.6.0-py3-none-any.whl
- Upload date:
- Size: 291.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.4.2 CPython/3.11.3 Darwin/23.2.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | be59fe9796d2a0d1ac37fb02a98ad1ffb7cde3a0707b03d3d64cd07e1f9f51aa |
|
MD5 | 800d304c626904094399faeb1911dc18 |
|
BLAKE2b-256 | 2010ca4c51b4393d55c62d0720c511146a3a7d28b431b34b537cd94eca3fc8f3 |