Skip to main content

Import/Merge CSV files into Notion database

Project description

csv2notion

PyPI version Python Version tests codecov

An alternative way to import *.csv files to Notion.so.

Due to current limitations of the official Notion SDK this tool is using the unofficial SDK by Jamie Alexandre notion-py.

Advantages over native import

  • Actually merge CSV with existing database rows (not just add new ones), first column will be used as a key
  • Manually set column types instead of relying on autodetection
  • Automatically link or create new entries in relation columns based on their value
  • Set icon for each row
  • Set cover or embed image for each row
  • Upload image file used for cover or icon
  • Options for validation of input data

Disadvantages over native import

  • Slower speed, since every row is imported separately
    • this is mitigated by multithreaded upload

Installation

Download the latest binary release for your OS.

With PIP

$ pip install csv2notion

Python 3.7 or later required.

Or, since csv2notion is a standalone tool, it might be more convenient to install it using pipx:

$ pipx install csv2notion

From source

This project uses poetry for dependency management and packaging. You will have to install it first. See poetry official documentation for instructions.

$ git clone https://github.com/vzhd1701/csv2notion.git
$ cd csv2notion/
$ poetry install --no-dev
$ poetry run csv2notion

Usage

$ csv2notion --help
usage: csv2notion [-h] --token TOKEN [--url URL] [--max-threads NUMBER] [--custom-types TYPES] [--image-column COLUMN] [--image-column-keep] [--image-column-mode {cover,block}] [--icon-column COLUMN] [--icon-column-keep]
                  [--missing-columns-action {add,ignore,fail}] [--missing-relations-action {add,ignore,fail}] [--fail-on-relation-duplicates] [--fail-on-duplicates] [--fail-on-duplicate-csv-columns] [--fail-on-conversion-error]
                  [--merge] [--merge-only-column COLUMN] [--mandatory-column COLUMN] [--log FILE] [--version]
                  FILE

Import/Merge CSV file into Notion database

positional arguments:
  FILE                  CSV file to upload

optional arguments:
  -h, --help            show this help message and exit
  --token TOKEN         Notion token, stored in token_v2 cookie for notion.so [NEEDED FOR UPLOAD]
  --url URL             Notion database URL; if none is provided, will create a new database
  --max-threads NUMBER  upload threads (default: 5)
  --custom-types TYPES  comma-separated list of custom types to use for non-key columns; if none is provided, types will be guessed from CSV values (used when creating a new database or --missing-columns-action is set to 'add')
  --image-column COLUMN
                        CSV column that points to URL or image file that will be embedded for that row
  --image-column-keep   keep image CSV column as a Notion DB column
  --image-column-mode {cover,block}
                        upload image as [cover] or insert it as [block] (default: block)
  --icon-column COLUMN  CSV column that points to emoji, URL or image file that will be used as page icon for that row
  --icon-column-keep    keep icon CSV column as a Notion DB column
  --missing-columns-action {add,ignore,fail}
                        if columns are present in CSV but not in Notion DB, [add] them to Notion DB, [ignore] them or [fail] (default: ignore)
  --missing-relations-action {add,ignore,fail}
                        if entries are missing from linked Notion DB, [add] them to Notion DB, [ignore] them or [fail] (default: ignore)
  --fail-on-relation-duplicates
                        fail if any linked DBs in relation columns have duplicate entries; otherwise, first entry in alphabetical order will be treated as unique when looking up relations
  --fail-on-duplicates  fail if Notion DB or CSV has duplicates in key column, useful when sanitizing before merge to avoid ambiguous mapping
  --fail-on-duplicate-csv-columns
                        fail if CSV has duplicate columns; otherwise last column will be used
  --fail-on-conversion-error
                        fail if any column type conversion error occurs; otherwise errors will be replaced with empty strings
  --merge               merge CSV with existing Notion DB rows, first column will be used as a key
  --merge-only-column COLUMN
                        CSV column that should be updated on merge; when provided, other columns will be ignored (define multiple times for multiple columns)
  --mandatory-column COLUMN
                        CSV column that cannot be empty (define multiple times for multiple columns)
  --log FILE            file to store program log
  --version             show program's version number and exit

Input

You must pass a single *.csv file for upload. The CSV file must contain at least 2 rows. The first row will be used as a header.

Optionally you can provide a URL to an existing Notion database with the --url option; if not provided, the tool will create a new database named after the CSV file. The URL must link to a database view, not a page.

The tool also requires you to have a token_v2 cookie for the Notion website. For information on how to get it, see this article.

Upload speed

Due to API limitations, the upload is performed one row at a time. To speed things up, this tool uses multiple parallel threads. Use the --max-threads option to control how fast it will go. Try not to set it too high to avoid rate limiting by the Notion server.

Duplicate CSV columns

Notion does not allow the database to have multiple columns with the same name. Therefore CSV columns will be treated as unique. Only the last column will be used if CSV has multiple columns with the same name. If you want the program to stop if it finds duplicate columns, use the --fail-on-duplicate-csv-columns flag.

Missing columns

If a CSV file has columns not present in Notion DB, they will be ignored by default. You can change this by using the --missing-columns-action option. Set it to add if you want the tool to add missing columns into Notion DB. Set to fail if you want the program to stop if it finds a column mismatch.

Column types

By default, the tool will try to guess column types based on their content. Alternatively, you can provide a comma-separated list of column types with the --custom-types option when creating a new database or adding new columns with the --missing-columns-action option set to add. Since the first column in Notion DB is always text, the tool will use the list to set types for the rest of the columns.

If the tool cannot convert the column value type properly, it will replace it with an empty string. If you want to make sure all values are correctly converted, use the --fail-on-conversion-error flag, which will stop execution in case of a conversion error.

Merging

By default, the tool will add rows to the existing Notion DB. To merge CSV rows with the Notion database, use the --merge flag. The first column of CSV and Notion DB will be used as a key to update existing rows with new values. CSV rows that didn't have a match in Notion DB will be added as new.

Since the tool treats rows as unique during merge based on the key column, it will use first found rows with a unique key. To avoid this ambiguity, you might want to validate duplicate row keys with the --fail-on-duplicates flag. It will check both CSV and target Notion DB before the merge.

If you want only select columns to be updated, use the --merge-only-column option.

Relation columns

Notion database has a relation column type, which allows you to link together entries from different databases. The tool will try to match column data with keys from a linked database.

By default, it will match with the first found row; if it cannot find the match, it will add nothing. You can change this behavior with the --missing-relations-action option. Set it to add if you want the tool to create new entries in the linked DB if no match is found. Set it to fail if you want the program to stop if no match is found.

Since the tool treats rows in the linked DB as unique you can prevent ambiguous matching with the --fail-on-relation-duplicates flag. It will check linked DB for duplicate keys and stop the executions if it finds any.

Cover image / Embedded image

The tool allows you to add an image to each row with the --image-column option. It will use one column from CSV as a data source for the image. It can be either a URL or a file name. The file name must be either an absolute path or a path relative to the CSV file. The tool will upload the file to the Notion server.

By default, the tool will embed an image inside the row page. If you want it to use the image as a page cover, then set the --image-column-mode option to cover.

Column specified with the --image-column option will not be treated as a regular column by default. If you want it to appear in Notion DB, use the --image-column-keep flag.

Icon

The tool allows you to add an icon to each row with the --icon-column option. The behavior is the same as with --image-column; the only difference is that you can use URL, file name, or single emoji.

To also treat --icon-column as a regular column, use --icon-column-keep flag, similar to --image-column-keep.

Mandatory columns

If you want to ensure that specific columns always have value and are not allowed to be empty, then use the --mandatory-column option. The program execution will stop if validation fails.

Examples

Getting help

If you found a bug or have a feature request, please open a new issue.

If you have a question about the program or have difficulty using it, you are welcome to the discussions page. You can also mail me directly, I'm always happy to help.

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

csv2notion-0.1.0.tar.gz (50.5 kB view details)

Uploaded Source

Built Distribution

csv2notion-0.1.0-py3-none-any.whl (54.9 kB view details)

Uploaded Python 3

File details

Details for the file csv2notion-0.1.0.tar.gz.

File metadata

  • Download URL: csv2notion-0.1.0.tar.gz
  • Upload date:
  • Size: 50.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.11 CPython/3.8.12 Linux/5.13.0-1022-azure

File hashes

Hashes for csv2notion-0.1.0.tar.gz
Algorithm Hash digest
SHA256 63504f13a7e40ba97618e7ba652cdef87bf46ebd57047a5c656eb4c996580f3b
MD5 4054f3ea7e78589c958f37e0b85faff9
BLAKE2b-256 655f6793f19369d218368723b152e7aca59a46d30a3478c15bb488850833f7ab

See more details on using hashes here.

File details

Details for the file csv2notion-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: csv2notion-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 54.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.11 CPython/3.8.12 Linux/5.13.0-1022-azure

File hashes

Hashes for csv2notion-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 63a7676c3f02e066df80be7218379198890859612ea76f3e856585f6df7de91d
MD5 fed458c830c28ca255d18777800468fe
BLAKE2b-256 3108c6af861e14cc42ed82adff7ff7ece93cfdf3c27022d95975fee0a947350f

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