Created
September 22, 2018 11:54
-
-
Save vinayak-mehta/b042ce545779cf7565e6530036e7a9de to your computer and use it in GitHub Desktop.
A Python2 script to extract tables from a PDF file using pdf-table-extract; saves tables as CSV files inside the current working directory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Usage: python pdf_table_extract.py <filename> | |
""" | |
import os | |
import sys | |
import pandas as pd | |
import pdftableextract as pdf | |
root, ext = os.path.splitext(os.path.basename(sys.argv[1])) | |
if ext.lower() != '.pdf': | |
raise ValueError('This script works only with PDF files.') | |
pages = ['1'] | |
cells = [pdf.process_page(sys.argv[1], p) for p in pages] | |
cells = [cell for row in cells for cell in row] | |
tables = pdf.table_to_list(cells, pages) | |
for i, table in enumerate(tables[1:]): | |
df = pd.DataFrame(table) | |
out = '{}-page-1-table-{}.csv'.format(root, i + 1) | |
df.to_csv(out, index=False, quoting=1, encoding='utf-8') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I dont see a pip module named 'pdftableextract' unable to download it with
pip install pdftableextract