diff --git a/camelot/cli.py b/camelot/cli.py index a2b45a50..b6615556 100644 --- a/camelot/cli.py +++ b/camelot/cli.py @@ -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, diff --git a/camelot/core.py b/camelot/core.py index 4e5869af..e82a11ff 100644 --- a/camelot/core.py +++ b/camelot/core.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- import os +import sqlite3 import zipfile import tempfile from itertools import chain @@ -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 @@ -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. @@ -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)) diff --git a/docs/user/quickstart.rst b/docs/user/quickstart.rst index 959c0d49..144a302e 100644 --- a/docs/user/quickstart.rst +++ b/docs/user/quickstart.rst @@ -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() ` method. Alternatively you can use :meth:`to_json() `, :meth:`to_excel() ` or :meth:`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() ` method. Alternatively you can use :meth:`to_json() `, :meth:`to_excel() ` :meth:`to_html() ` or :meth:`to_sqlite() ` methods to export the table as JSON, Excel, HTML files or a sqlite database respectively. :: @@ -76,7 +76,7 @@ You can also export all tables at once, using the :class:`tables ` 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.