I have an xml file which is output for Abby's OCR software.
The xml content can contain multiple text blocks and also multiple table blocks (As the OCR'd document looks similar to a research paper which contains paragraphs of text with some tables in between those paragraphs)
I am trying to extract the text from tables, which looks like this in the xml:
<ComposedBlock ID="Page1_Block4" HEIGHT="240" WIDTH="1170" VPOS="226" HPOS="143" TYPE="table">
<TextBlock ID="Page1_Block5" HEIGHT="55" WIDTH="393" VPOS="226" HPOS="143" LANG="en-US" STYLEREFS="StyleId-E6BF91A3-3D6A-442F-9A46-22A0459A02E9- font1">
<TextLine HEIGHT="33" WIDTH="178" VPOS="234" HPOS="154">
<String CONTENT="some text" HEIGHT="33" WIDTH="178" VPOS="234" HPOS="154"/>
</TextLine>
</TextBlock>
The non-table text is present as part of text blocks, an example below:
<TextBlock ID="Page1_Block1" HEIGHT="52" WIDTH="1918" VPOS="3101" HPOS="148" STYLEREFS="StyleId-4CD32088-9994-4ED5-BD2B-8082FC83356D- font1" LANG="en-US">
<TextLine HEIGHT="42" WIDTH="1362" VPOS="3101" HPOS="154">
<String CONTENT="Mafi" HEIGHT="32" WIDTH="74" VPOS="3101" HPOS="154"/>
<String STYLE="bold" CONTENT="," HEIGHT="10" WIDTH="4" VPOS="3129" HPOS="235"/>
<SP HEIGHT="36" WIDTH="18" VPOS="3103" HPOS="240"/>
</TextLine>
</TextBlock>
Now, my problem statement is: How do I get the global offsets for only the text in the tables?
This is how my code looks like for extracting text from tables:
soup = bs.BeautifulSoup(content,'lxml')
page_definition = "Page" + str(page) + "_"
tables = soup.find_all('composedblock',{"type": "table"}, id=lambda value: value and value.startswith(page_definition))
for table in tables:
table_content = []
vpos = []
# Converting table xml to str for bs4 to be able to consume
table = str(table)
xml_soup = bs.BeautifulSoup(table, 'lxml')
# Finding all <textblock> tags inside the table ComposedBlock
text_blocks = xml_soup.find_all('textblock')
# Looping over all text blocks inside the table
for text_block in text_blocks:
print(table.text.index(text_block.text))
# Getting all vpos numbers, as different vpos signify different rows ..
# .. due to the virtue of vertical positions
vpos.append(text_block.get("vpos"))
# Collecting all vpos of a table
all_vpos = set(vpos)
all_vpos = sorted(list(all_vpos))
for vpos in all_vpos:
txt_block_row = xml_soup.find_all("textblock", {"vpos": str(vpos)})
# Finding all text in a single
row = []
for txt_block in txt_block_row:
texts = txt_block.findAll("string")
content_text =[]
for text in texts:
content = text.get('content')