commit
2f262453a7
|
|
@ -36,7 +36,7 @@ pass_config = click.make_pass_decorator(Config)
|
||||||
@click.option('-pw', '--password', help='Password for decryption.')
|
@click.option('-pw', '--password', help='Password for decryption.')
|
||||||
@click.option('-o', '--output', help='Output file path.')
|
@click.option('-o', '--output', help='Output file path.')
|
||||||
@click.option('-f', '--format',
|
@click.option('-f', '--format',
|
||||||
type=click.Choice(['csv', 'json', 'excel', 'html']),
|
type=click.Choice(['csv', 'json', 'excel', 'html', 'sqlite']),
|
||||||
help='Output file format.')
|
help='Output file format.')
|
||||||
@click.option('-z', '--zip', is_flag=True, help='Create ZIP archive.')
|
@click.option('-z', '--zip', is_flag=True, help='Create ZIP archive.')
|
||||||
@click.option('-split', '--split_text', is_flag=True,
|
@click.option('-split', '--split_text', is_flag=True,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import sqlite3
|
||||||
import zipfile
|
import zipfile
|
||||||
import tempfile
|
import tempfile
|
||||||
from itertools import chain
|
from itertools import chain
|
||||||
|
|
@ -592,6 +593,28 @@ class Table(object):
|
||||||
with open(path, 'w') as f:
|
with open(path, 'w') as f:
|
||||||
f.write(html_string)
|
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):
|
class TableList(object):
|
||||||
"""Defines a list of camelot.core.Table objects. Each table can
|
"""Defines a list of camelot.core.Table objects. Each table can
|
||||||
|
|
@ -656,7 +679,7 @@ class TableList(object):
|
||||||
path : str
|
path : str
|
||||||
Output filepath.
|
Output filepath.
|
||||||
f : str
|
f : str
|
||||||
File format. Can be csv, json, excel and html.
|
File format. Can be csv, json, excel, html and sqlite.
|
||||||
compress : bool
|
compress : bool
|
||||||
Whether or not to add files to a ZIP archive.
|
Whether or not to add files to a ZIP archive.
|
||||||
|
|
||||||
|
|
@ -689,3 +712,11 @@ class TableList(object):
|
||||||
zipname = os.path.join(os.path.dirname(path), root) + '.zip'
|
zipname = os.path.join(os.path.dirname(path), root) + '.zip'
|
||||||
with zipfile.ZipFile(zipname, 'w', allowZip64=True) as z:
|
with zipfile.ZipFile(zipname, 'w', allowZip64=True) as z:
|
||||||
z.write(filepath, os.path.basename(filepath))
|
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))
|
||||||
|
|
|
||||||
|
|
@ -56,7 +56,7 @@ Woah! The accuracy is top-notch and there is less whitespace, which means the ta
|
||||||
.. csv-table::
|
.. csv-table::
|
||||||
:file: ../_static/csv/foo.csv
|
: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.
|
||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
|
@ -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
|
$ 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.
|
.. 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.
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue