CoolFace
Apppublic

Surya152002/appa

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py59 linesDownload Raw Back to root
1import tabula2from docx import Document3import cv24import pytesseract5import pandas as pd6pytesseract.pytesseract.tesseract_cmd = r'./tesseract.exe'  # Change the path accordingly7 8 9 10def extract_tables_from_pdf(file_path):11    return tabula.read_pdf(file_path, pages="all", multiple_tables=True)12 13 14def extract_tables_from_image(image_path):15    image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)16    text = pytesseract.image_to_string(image)17    18    # Convert the extracted text to a dataframe (assuming one table in the image)19    # The logic may vary based on the nature of your table20    rows = text.split('\n')21    data = [row.split() for row in rows if row]22    df = pd.DataFrame(data[1:], columns=data[0])23 24    return [df]  # Returning as a list to be consistent with the PDF extraction function25 26 27# Decide extractor based on file extension28file_path = "./1234.jpg"  # Change the extension to test29file_extension = file_path.split('.')[-1].lower()30 31if file_extension == "pdf":32    tables = extract_tables_from_pdf(file_path)33elif file_extension in ["jpg", "jpeg", "png"]:34    tables = extract_tables_from_image(file_path)35else:36    raise ValueError(f"Unsupported file format: {file_extension}")37 38 39# Create a new Word document40doc = Document()41 42# Iterate through the extracted tables43for table_df in tables:44    # Add table to Word document45    t = doc.add_table(rows=1, cols=table_df.shape[1])46    hdr_cells = t.rows[0].cells47    for i, column in enumerate(table_df.columns):48        hdr_cells[i].text = str(column)49 50    for index, row in table_df.iterrows():51        cells = t.add_row().cells52        for i, value in enumerate(row):53            cells[i].text = str(value)54 55# Save the Word document56doc.save("output.docx")57 58print("Tables exported to output.docx!")59