NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

import webbrowser
import json
import boto3
import io
from io import BytesIO
from pdf2image import convert_from_path
from pprint import pprint

def pdf_to_image(pdf_path):
# Convert PDF to image using pdf2image
images = convert_from_path(pdf_path, fmt='png')
return images

def get_rows_columns_map(table_result, blocks_map):
rows = {}
scores = []
for relationship in table_result['Relationships']:
if relationship['Type'] == 'CHILD':
for child_id in relationship['Ids']:
cell = blocks_map[child_id]
if cell['BlockType'] == 'CELL':
row_index = cell['RowIndex']
col_index = cell['ColumnIndex']
if row_index not in rows:
# create new row
rows[row_index] = {}

# get confidence score
scores.append(str(cell['Confidence']))

# get the text value
rows[row_index][col_index] = get_text(cell, blocks_map)
return rows, scores

def get_text(result, blocks_map):
text = ''
if 'Relationships' in result:
for relationship in result['Relationships']:
if relationship['Type'] == 'CHILD':
for child_id in relationship['Ids']:
word = blocks_map[child_id]
if word['BlockType'] == 'WORD':
if "," in word['Text'] and word['Text'].replace(",", "").isnumeric():
text += '"' + word['Text'] + '"' + ' '
else:
text += word['Text'] + ' '
if word['BlockType'] == 'SELECTION_ELEMENT':
if word['SelectionStatus'] =='SELECTED':
text += 'X '
return text

def get_table_text_results(image_bytes):
session = boto3.Session(profile_name=None)
client = session.client('textract', region_name='us-east-2', aws_access_key_id="AKIATBS6HHWP4WL7Q6XI", aws_secret_access_key="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")

response = client.analyze_document(Document={'Bytes': image_bytes}, FeatureTypes=['TABLES'])

blocks = response['Blocks']
blocks_map = {}
table_blocks = []

for block in blocks:
blocks_map[block['Id']] = block
if block['BlockType'] == "TABLE":
table_blocks.append(block)

if len(table_blocks) <= 0:
return ""

text = ''
for index, table in enumerate(table_blocks):
rows, scores = get_rows_columns_map(table, blocks_map)
text += generate_table_text(rows, scores)
text += 'nn'

return text

def generate_table_text(rows, scores):
text = ''

for row_index, cols in rows.items():
for col_index, cell_text in cols.items():
text += f'{cell_text} '
text += 'n'

# text += 'nn Confidence Scores % (Table Cell) n'
# cols_count = 0
# for score in scores:
# cols_count += 1
# text += f'{score},'
# if cols_count == len(scores):
# text += 'n'
# cols_count = 0

text += 'nnn'

return text


def main(pdf_path, output_file='output.txt'):
images = pdf_to_image(pdf_path)

all_text = ''

for i, image in enumerate(images):
image_bytes = BytesIO()
image.save(image_bytes, format='PNG')
image_bytes = image_bytes.getvalue()

table_text = get_table_text_results(image_bytes)
table_text = table_text[:table_text.find('Confidence Scores % (Table Cell)')]
all_text += table_text

with open(output_file, "wt") as fout:
fout.write(all_text)

print('Text OUTPUT FILE: ', all_text)
return all_text

if __name__ == "__main__":
pdf_path = '/content/06-2023 3590.pdf'
main(pdf_path)
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.