diff --git a/docreader/parser/docx_converter.py b/docreader/parser/docx_converter.py new file mode 100644 index 00000000000..f1b5ad6afcd --- /dev/null +++ b/docreader/parser/docx_converter.py @@ -0,0 +1,35 @@ +"""Keep DOCX table cell boundaries intact in MarkItDown's Markdown tables.""" + +from bs4 import BeautifulSoup +from markitdown.converters import DocxConverter, HtmlConverter + +from docreader.parser.mhtml_parser import MHTMLParser + + +class _EscapingHtmlConverter(HtmlConverter): + """Escape table cell pipes before markdownify turns the HTML into a table. + + MarkItDown converts a DOCX to HTML with mammoth and then writes the table + with markdownify, which leaves a ``|`` in a cell's text as it is. The + reader then takes that pipe for a column delimiter, so every cell after it + shifts one column right and the row gains a column. MarkItDown's PPTX and + CSV converters are wrapped in this package for the same reason; a DOCX + goes through this converter instead. + """ + + def convert_string(self, html_content, **kwargs): + # Unchanged behaviour (and no parse cost) for documents without a + # table, which is the common case. + if "