From 955e4b62d0ca021b97550c33915eac50982bf742 Mon Sep 17 00:00:00 2001 From: Lucas Cimon <925560+Lucas-C@users.noreply.github.com> Date: Tue, 12 Jan 2021 15:38:07 +0100 Subject: [PATCH] New export format: markdown --- camelot/cli.py | 2 +- camelot/core.py | 19 +++++++++++++++++-- setup.py | 3 ++- tests/test_cli.py | 16 ++++++++++++---- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/camelot/cli.py b/camelot/cli.py index 1715e6f..546a32d 100644 --- a/camelot/cli.py +++ b/camelot/cli.py @@ -43,7 +43,7 @@ pass_config = click.make_pass_decorator(Config) @click.option( "-f", "--format", - type=click.Choice(["csv", "json", "excel", "html", "sqlite"]), + type=click.Choice(["csv", "excel", "html", "json", "markdown", "sqlite"]), help="Output file format.", ) @click.option("-z", "--zip", is_flag=True, help="Create ZIP archive.") diff --git a/camelot/core.py b/camelot/core.py index fdc2ae3..2a0ddd9 100644 --- a/camelot/core.py +++ b/camelot/core.py @@ -634,6 +634,21 @@ class Table(object): with open(path, "w", encoding="utf-8") as f: f.write(html_string) + def to_markdown(self, path, **kwargs): + """Writes Table to a Markdown file. + + For kwargs, check :meth:`pandas.DataFrame.to_markdown`. + + Parameters + ---------- + path : str + Output filepath. + + """ + md_string = self.df.to_markdown(**kwargs) + with open(path, "w", encoding="utf-8") as f: + f.write(md_string) + def to_sqlite(self, path, **kwargs): """Writes Table to sqlite database. @@ -715,7 +730,7 @@ class TableList(object): path : str Output filepath. f : str - File format. Can be csv, json, excel, html and sqlite. + File format. Can be csv, excel, html, json, markdown or sqlite. compress : bool Whether or not to add files to a ZIP archive. @@ -728,7 +743,7 @@ class TableList(object): kwargs = {"path": path, "dirname": dirname, "root": root, "ext": ext} - if f in ["csv", "json", "html"]: + if f in ["csv", "html", "json", "markdown"]: self._write_file(f=f, **kwargs) if compress: self._compress_dir(**kwargs) diff --git a/setup.py b/setup.py index b1ac666..25bd612 100644 --- a/setup.py +++ b/setup.py @@ -20,7 +20,8 @@ requires = [ 'openpyxl>=2.5.8', 'pandas>=0.23.4', 'pdfminer.six>=20200726', - 'PyPDF2>=1.26.0' + 'PyPDF2>=1.26.0', + 'tabulate' ] cv_requires = [ diff --git a/tests/test_cli.py b/tests/test_cli.py index f897315..a496be6 100755 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -123,7 +123,7 @@ def test_cli_output_format(): cli, ["--format", "json", "--output", outfile, "stream", infile], ) - assert result.exit_code == 0 + assert result.exit_code == 0, f"Output: {result.output}" # excel outfile = os.path.join(tempdir, "health.xlsx") @@ -131,7 +131,7 @@ def test_cli_output_format(): cli, ["--format", "excel", "--output", outfile, "stream", infile], ) - assert result.exit_code == 0 + assert result.exit_code == 0, f"Output: {result.output}" # html outfile = os.path.join(tempdir, "health.html") @@ -139,7 +139,15 @@ def test_cli_output_format(): cli, ["--format", "html", "--output", outfile, "stream", infile], ) - assert result.exit_code == 0 + assert result.exit_code == 0, f"Output: {result.output}" + + # markdown + outfile = os.path.join(tempdir, "health.md") + result = runner.invoke( + cli, + ["--format", "markdown", "--output", outfile, "stream", infile], + ) + assert result.exit_code == 0, f"Output: {result.output}" # zip outfile = os.path.join(tempdir, "health.csv") @@ -155,7 +163,7 @@ def test_cli_output_format(): infile, ], ) - assert result.exit_code == 0 + assert result.exit_code == 0, f"Output: {result.output}" def test_cli_quiet():