fix(docx): slow table parsing (#2553)

* chore(docx): remove unnecessary import

Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>

* fix(docx): simplify parsing of simple tables

Simplify the parsing of tables with just text (no rich cells).
Move nested function group_cell_elements out of _handle_tables for readability.

Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>

* chore(docx): reuse method for finding inline pictures

Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>

* chore(docx): format strikethrough text

Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>

* tests(docx): use fixtures to avoid converting same file multiple times

Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>

* fix(docx): remove unnecessary argument docx_obj in functions

Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>

* tests(docx): add test for rich table cells

Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>

* chore(docx): small improvements in backend and its unit tests

Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>

* chore(docx): parse superscript and subscript formatted text

Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>

---------

Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>
This commit is contained in:
Cesar Berrospi Ramis
2025-11-06 05:25:53 +01:00
committed by GitHub
parent 0ba8d5d9e3
commit ef623ffcee
6 changed files with 3366 additions and 218 deletions

BIN
tests/data/docx/docx_rich_cells.docx vendored Normal file

Binary file not shown.

View File

@@ -0,0 +1,107 @@
item-0 at level 0: unspecified: group _root_
item-1 at level 1: section: group header-0
item-2 at level 2: section: group header-1
item-3 at level 3: section_header: Table with rich cells
item-4 at level 4: table with [4x2]
item-5 at level 5: unspecified: group rich_cell_group_1_0_1
item-6 at level 6: text: This is a list:
item-7 at level 6: list: group list
item-8 at level 7: list_item: A First
item-9 at level 7: list_item: A Second
item-10 at level 7: list_item: A Third
item-11 at level 5: unspecified: group rich_cell_group_1_1_1
item-12 at level 6: text: This is a formatted list:
item-13 at level 6: list: group list
item-14 at level 7: list_item:
item-15 at level 8: inline: group group
item-16 at level 9: text: B
item-17 at level 9: text: First
item-18 at level 7: list_item:
item-19 at level 8: inline: group group
item-20 at level 9: text: B
item-21 at level 9: text: Second
item-22 at level 7: list_item:
item-23 at level 8: inline: group group
item-24 at level 9: text: B
item-25 at level 9: text: Third
item-26 at level 5: unspecified: group rich_cell_group_1_0_2
item-27 at level 6: text: First Paragraph
Second Paragraph
item-28 at level 6: text: Third paragraph before a numbered list
item-29 at level 6: list: group list
item-30 at level 7: list_item: Number one
item-31 at level 7: list_item: Number two
item-32 at level 7: list_item: Number three
item-33 at level 5: unspecified: group rich_cell_group_1_1_2
item-34 at level 6: text: This is simple text with
item-35 at level 6: text: bold
item-36 at level 6: text: ,
item-37 at level 6: text: strikethrough
item-38 at level 6: text: and
item-39 at level 6: text: italic
item-40 at level 6: text: formatting with x
item-41 at level 6: text: 2
item-42 at level 6: text: and H
item-43 at level 6: text: 2
item-44 at level 6: text: O
item-45 at level 5: unspecified: group rich_cell_group_1_0_3
item-46 at level 6: text: This is a paragraph
item-47 at level 6: text: This is another paragraph
item-48 at level 4: inline: group group
item-49 at level 4: text:
item-50 at level 4: text:
item-51 at level 4: text:
item-52 at level 4: text:
item-53 at level 4: text:
item-54 at level 4: text:
item-55 at level 3: section_header: Table with nested table
item-56 at level 4: text: Before table
item-57 at level 4: table with [3x2]
item-58 at level 5: unspecified: group rich_cell_group_2_1_1
item-59 at level 6: text: Simple cell with
item-60 at level 6: text: bold
item-61 at level 6: text: and
item-62 at level 6: text: italic
item-63 at level 6: text: text
item-64 at level 5: unspecified: group rich_cell_group_3_0_2
item-65 at level 6: table with [2x3]
item-66 at level 7: unspecified: group rich_cell_group_3_0_1
item-67 at level 8: text: Cell 1
item-68 at level 7: unspecified: group rich_cell_group_3_1_1
item-69 at level 8: text: Cell 2
item-70 at level 7: unspecified: group rich_cell_group_3_2_1
item-71 at level 8: text: Cell 3
item-72 at level 6: text:
item-73 at level 5: unspecified: group rich_cell_group_4_1_2
item-74 at level 6: text: Rich cell
A nested table
item-75 at level 6: table with [2x3]
item-76 at level 7: unspecified: group rich_cell_group_4_0_1
item-77 at level 8: text: Cell 1
item-78 at level 7: unspecified: group rich_cell_group_4_1_1
item-79 at level 8: text: Cell 2
item-80 at level 7: unspecified: group rich_cell_group_4_2_1
item-81 at level 8: text: Cell 3
item-82 at level 6: text:
item-83 at level 4: inline: group group
item-84 at level 4: inline: group group
item-85 at level 5: text: After table with
item-86 at level 5: text: bold
item-87 at level 5: text: ,
item-88 at level 5: text: underline
item-89 at level 5: text: ,
item-90 at level 5: text: strikethrough
item-91 at level 5: text: , and
item-92 at level 5: text: italic
item-93 at level 5: text: formatting
item-94 at level 4: text:
item-95 at level 3: section_header: Table with pictures
item-96 at level 4: text:
item-97 at level 4: table with [3x2]
item-98 at level 5: unspecified: group rich_cell_group_5_1_1
item-99 at level 6: picture
item-100 at level 5: unspecified: group rich_cell_group_5_0_2
item-101 at level 6: text: Text and picture
item-102 at level 6: picture
item-103 at level 4: text:

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,25 @@
### Table with rich cells
| Column A | Column B |
|------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------|
| This is a list: - A First - A Second - A Third | This is a formatted list: - B **First** - B *Second* - B Third |
| First Paragraph Second Paragraph Third paragraph before a numbered list 1. Number one 2. Number two 3. Number three | This is simple text with **bold** , ~~strikethrough~~ and *italic* formatting with x 2 and H 2 O |
| This is a paragraph This is another paragraph | |
### Table with nested table
Before table
| Column A | Column B |
|----------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------|
| Simple cell upper left | Simple cell with **bold** and *italic* text |
| | A | B | C | |----------|--------|------------| | *Cell 1* | Cell 2 | **Cell 3** | | Rich cell A nested table | A | B | C | |------------|--------------|--------| | ~~Cell 1~~ | ***Cell 2*** | Cell 3 | |
After table with **bold** , underline , ~~strikethrough~~ , and *italic* formatting
### Table with pictures
| Column A | Column B |
|----------------------------------|----------------|
| Only text | <!-- image --> |
| Text and picture <!-- image --> | |