Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

[MRG] Add sqlite support #244

Merged
merged 1 commit into from
Jan 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion camelot/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def set_config(self, key, value):
@click.option('-pw', '--password', help='Password for decryption.')
@click.option('-o', '--output', help='Output file path.')
@click.option('-f', '--format',
type=click.Choice(['csv', 'json', 'excel', 'html']),
type=click.Choice(['csv', 'json', 'excel', 'html', 'sqlite']),
help='Output file format.')
@click.option('-z', '--zip', is_flag=True, help='Create ZIP archive.')
@click.option('-split', '--split_text', is_flag=True,
Expand Down
33 changes: 32 additions & 1 deletion camelot/core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

import os
import sqlite3
import zipfile
import tempfile
from itertools import chain
Expand Down Expand Up @@ -592,6 +593,28 @@ def to_html(self, path, **kwargs):
with open(path, 'w') as f:
f.write(html_string)

def to_sqlite(self, path, **kwargs):
"""Writes Table to sqlite database.

For kwargs, check :meth:`pandas.DataFrame.to_sql`.

Parameters
----------
path : str
Output filepath.

"""
kw = {
'if_exists': 'replace',
'index': False
}
kw.update(kwargs)
conn = sqlite3.connect(path)
table_name = 'page-{}-table-{}'.format(self.page, self.order)
self.df.to_sql(table_name, conn, **kw)
conn.commit()
conn.close()


class TableList(object):
"""Defines a list of camelot.core.Table objects. Each table can
Expand Down Expand Up @@ -656,7 +679,7 @@ def export(self, path, f='csv', compress=False):
path : str
Output filepath.
f : str
File format. Can be csv, json, excel and html.
File format. Can be csv, json, excel, html and sqlite.
compress : bool
Whether or not to add files to a ZIP archive.

Expand Down Expand Up @@ -689,3 +712,11 @@ def export(self, path, f='csv', compress=False):
zipname = os.path.join(os.path.dirname(path), root) + '.zip'
with zipfile.ZipFile(zipname, 'w', allowZip64=True) as z:
z.write(filepath, os.path.basename(filepath))
elif f == 'sqlite':
filepath = os.path.join(dirname, basename)
for table in self._tables:
table.to_sqlite(filepath)
if compress:
zipname = os.path.join(os.path.dirname(path), root) + '.zip'
with zipfile.ZipFile(zipname, 'w', allowZip64=True) as z:
z.write(filepath, os.path.basename(filepath))
4 changes: 2 additions & 2 deletions docs/user/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Woah! The accuracy is top-notch and there is less whitespace, which means the ta
.. csv-table::
:file: ../_static/csv/foo.csv

Looks good! You can now export the table as a CSV file using its :meth:`to_csv() <camelot.core.Table.to_csv>` method. Alternatively you can use :meth:`to_json() <camelot.core.Table.to_json>`, :meth:`to_excel() <camelot.core.Table.to_excel>` or :meth:`to_html() <camelot.core.Table.to_html>` methods to export the table as JSON, Excel and HTML files respectively.
Looks good! You can now export the table as a CSV file using its :meth:`to_csv() <camelot.core.Table.to_csv>` method. Alternatively you can use :meth:`to_json() <camelot.core.Table.to_json>`, :meth:`to_excel() <camelot.core.Table.to_excel>` :meth:`to_html() <camelot.core.Table.to_html>` or :meth:`to_sqlite() <camelot.core.Table.to_sqlite>` methods to export the table as JSON, Excel, HTML files or a sqlite database respectively.

::

Expand All @@ -76,7 +76,7 @@ You can also export all tables at once, using the :class:`tables <camelot.core.T

$ camelot --format csv --output foo.csv lattice foo.pdf

This will export all tables as CSV files at the path specified. Alternatively, you can use ``f='json'``, ``f='excel'`` or ``f='html'``.
This will export all tables as CSV files at the path specified. Alternatively, you can use ``f='json'``, ``f='excel'``, ``f='html'`` or ``f='sqlite'``.

.. note:: The :meth:`export() <camelot.core.TableList.export>` method exports files with a ``page-*-table-*`` suffix. In the example above, the single table in the list will be exported to ``foo-page-1-table-1.csv``. If the list contains multiple tables, multiple CSV files will be created. To avoid filling up your path with multiple files, you can use ``compress=True``, which will create a single ZIP file at your path with all the CSV files.

Expand Down