Package: amazon-textract-prettyprinter
Textract-PrettyPrinterProvides functions to format the output received from Textract in more easily consumable formats incl. CSV or Markdown.
amazon-textract-prettyprinterInstall>python-mpipinstallamazon-textract-prettyprinterMake sure your environment is setup with AWS credentials through configuration files or environment variables or an attached role. (https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)SamplesGet FORMS and TABLES as CSVfromtextractcaller.t_callimportcall_textract,Textract_Featuresfromtextractprettyprinter.t_pretty_printimportPretty_Print_Table_Format,Textract_Pretty_Print,get_stringtextract_json=call_textract(input_document=input_document,features=[Textract_Features.FORMS,Textract_Features.TABLES])print(get_string(textract_json=textract_json,table_format=Pretty_Print_Table_Format.csv,output_type=[Textract_Pretty_Print.TABLES,Textract_Pretty_Print.FORMS]))Get string for TABLES using the get_string methodfromtextractcaller.t_callimportcall_textract,Textract_Featuresfromtextractprettyprinter.t_pretty_printimportTextract_Pretty_Print,get_stringtextract_json=call_textract(input_document=input_document,features=[Textract_Features.TABLES])get_string(textract_json=textract_json,output_type=Textract_Pretty_Print.TABLES)Print out tables in LaTeX formatfromtextractcaller.t_callimportcall_textract,Textract_Featuresfromtextractprettyprinter.t_pretty_printimportTextract_Pretty_Print,get_stringtextract_json=call_textract(input_document=input_document,features=[Textract_Features.FORMS,Textract_Features.TABLES])get_tables_string(textract_json=textract_json,table_format=Pretty_Print_Table_Format.latex)Get linearized text from LAYOUT using get_text_from_layout_json methodGenerates a dictionary of linearized text from the Textract JSON response with LAYOUT, and optionally writes linearized plain text files to local file system or Amazon S3. It can take either per page JSON from AnalyzeDocument API, or a single combined JSON with all the pages created from StartDocumentAnalysis output JSONs.fromtextractcaller.t_callimportcall_textract,Textract_Featuresfromtextractprettyprinter.t_pretty_printimportget_text_from_layout_jsontextract_json=call_textract(input_document=input_document,features=[Textract_Features.LAYOUT,Textract_Features.TABLES])layout=get_text_from_layout_json(textract_json=textract_json)full_text=layout[1]print(full_text)In addition totextract_json, theget_text_from_layout_jsonfunction can take the following additional parameterstable_format(str, optional): Format of tables within the document. Supports all python-tabulate table formats. Seetabulatefor supported table formats. Defaults togrid.exclude_figure_text(bool, optional): If set to True, excludes text extracted from figures in the document. Defaults toFalse.exclude_page_header(bool, optional): If set to True, excludes the page header from the linearized text. Defaults toFalse.exclude_page_footer(bool, optional): If set to True, excludes the page footer from the linearized text. Defaults toFalse.exclude_page_number(bool, optional): If set to True, excludes the page number from the linearized text. Defaults toFalse.skip_table(bool, optional): If set to True, skips including the table in the linearized text. Defaults toFalse.save_txt_path(str, optional): Path to save the output linearized text to files. Either a local file system path or Amazon S3 path can be specified ins3://bucket_name/prefix/format. Files will be saved with<page_number>.txtnaming convention.generate_markdown(bool, optional): If set toTrue, generates markdown formatted linearized text. Defaults toFalse.Generate the layout.csv similar to the Textract Web ConsoleCustomers asked for the abilility to generate the layout.csv format, which can be downloaded when testing documents in the AWS Web Console.
The method ``get_layout_csv_from_trp2```` generates for each page a list of the entries:'Page number,'Layout,'Text,'Reading Order,'Confidence scorePage number: starting at 1, incrementing for eac pageLayout: the BlockType + a number indicating the sequence for this BlockType starting at 1 and for LAYOUT_LIST elements the string:  "- part of LAYOUT_LIST (index)" is addedText: except for LAYOUT_LIST and LAYOUT_FIGURE the underlying textReading Order: increasing int for each LAYOUT element starting with 0Confidence score: confidence in this being a LAYOUT elementthis can be used to generate a CSV (or another format). Below a sample how to generate a CSV.# taken from the test# generates the CSV in memoryfromtextractprettyprinterimportget_layout_csv_from_trp2withopen(<some_test_file>)asinput_fp:trp2_doc:TDocument=TDocumentSchema().load(json.load(input_fp))layout_csv=get_layout_csv_from_trp2(trp2_doc)csv_output=io.StringIO()csv_writer=csv.writer(csv_output,delimiter=",",quotechar='"',quoting=csv.QUOTE_MINIMAL)forpageinlayout_csv:csv_writer.writerows(page)print(csv_output)Sample outputPage numberLayoutTextReading OrderBlockTypeConfidence score1LAYOUT_SECTION_HEADER 1Amazing Headline!...0LAYOUT_SECTION_HEADER81.251LAYOUT_TEXT 1Lorem ipsum dolor sit amet, co...1LAYOUT_TEXT99.7558593751LAYOUT_SECTION_HEADER 2Unbelievable stuff...2LAYOUT_SECTION_HEADER90.4785156251LAYOUT_TEXT 2Ut ultrices felis vel mi susci...3LAYOUT_TEXT98.4863281251LAYOUT_LIST 14LAYOUT_LIST97.167968751LAYOUT_TEXT 3 - part of LAYOUT_LIST 1Priority list item 1...5LAYOUT_TEXT97.85156251LAYOUT_TEXT 4 - part of LAYOUT_LIST 1Priority list item 2...6LAYOUT_TEXT98.0957031251LAYOUT_TEXT 5 - part of LAYOUT_LIST 1Another list item 3...7LAYOUT_TEXT98.0957031251LAYOUT_TEXT 6 - part of LAYOUT_LIST 1And a total optional list item...8LAYOUT_TEXT98.730468751LAYOUT_LIST 29LAYOUT_LIST69.531251LAYOUT_TEXT 7 - part of LAYOUT_LIST 21. But we...10LAYOUT_TEXT95.7519531251LAYOUT_TEXT 8 - part of LAYOUT_LIST 22. can also...11LAYOUT_TEXT96.9238281251LAYOUT_TEXT 9 - part of LAYOUT_LIST 23. do numbered...12LAYOUT_TEXT97.363281251LAYOUT_TEXT 10 - part of LAYOUT_LIST 24. lists...13LAYOUT_TEXT96.67968751LAYOUT_TEXT 11congue ac. Phasellus mollis co...14LAYOUT_TEXT96.0449218751LAYOUT_TEXT 12Quisque a elementum diam. Null...15LAYOUT_TEXT96.4843751LAYOUT_TEXT 13Table Caption 1...16LAYOUT_TEXT86.8652343751LAYOUT_TABLE 1Date Description Amount 12-12-...17LAYOUT_TABLE96.4355468751LAYOUT_TEXT 14Quisque dapibus varius ipsum, ...18LAYOUT_TEXT93.066406251LAYOUT_FIGURE 119LAYOUT_FIGURE94.33593751LAYOUT_TEXT 15Figure Caption 1...20LAYOUT_TEXT63.18359375