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 65fd1a6..63ddc15 100644 --- a/camelot/core.py +++ b/camelot/core.py @@ -630,6 +630,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. @@ -711,7 +726,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. @@ -724,7 +739,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 1e1b4e6..4ac7e0a 100644 --- a/setup.py +++ b/setup.py @@ -21,6 +21,7 @@ requires = [ "pandas>=0.23.4", "pdfminer.six>=20200726", "PyPDF2>=1.26.0", + "tabulate>=0.8.9", ] cv_requires = ["opencv-python>=3.4.2.17"] 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():