adrien.aribaut-gaudin
commited on
Commit
·
445a883
1
Parent(s):
8e58322
fix: table insertion with index
Browse files- src/domain/doc.py +22 -5
src/domain/doc.py
CHANGED
|
@@ -426,7 +426,8 @@ class Doc:
|
|
| 426 |
log_dict = {'list_mapping': log}
|
| 427 |
return log_dict
|
| 428 |
|
| 429 |
-
def table_insertion(self,
|
|
|
|
| 430 |
#the content is the content of the table with the following format:
|
| 431 |
#content = {
|
| 432 |
# "header": ["header1", "header2", "header3"],
|
|
@@ -436,9 +437,26 @@ class Doc:
|
|
| 436 |
# ["row3", "row3", "row3"],
|
| 437 |
# ]
|
| 438 |
#}
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 442 |
#add the header
|
| 443 |
for i, header in enumerate(content["headers"]):
|
| 444 |
table.cell(0, i).text = header
|
|
@@ -448,7 +466,6 @@ class Doc:
|
|
| 448 |
table.cell(i+1, j).text = cell
|
| 449 |
#insert the table after the paragraph
|
| 450 |
self.move_table_after(table, paragraph.xparagraph)
|
| 451 |
-
self.rearrange_tables()
|
| 452 |
#save the doc
|
| 453 |
self.save_as_docx()
|
| 454 |
return table
|
|
|
|
| 426 |
log_dict = {'list_mapping': log}
|
| 427 |
return log_dict
|
| 428 |
|
| 429 |
+
def table_insertion(self, index: str, content: dict):
|
| 430 |
+
#the index is the index of the block in the docx file where to insert the table
|
| 431 |
#the content is the content of the table with the following format:
|
| 432 |
#content = {
|
| 433 |
# "header": ["header1", "header2", "header3"],
|
|
|
|
| 437 |
# ["row3", "row3", "row3"],
|
| 438 |
# ]
|
| 439 |
#}
|
| 440 |
+
list_of_indexes = index.split(".")
|
| 441 |
+
index_in_list = [eval(i) for i in list_of_indexes]
|
| 442 |
+
#find the container which has the index
|
| 443 |
+
paragraph : Paragraph = None
|
| 444 |
+
containers : [Container] = self.container.containers
|
| 445 |
+
for c in containers:
|
| 446 |
+
if c.index == index_in_list:
|
| 447 |
+
if c.title:
|
| 448 |
+
paragraph = c.title
|
| 449 |
+
else:
|
| 450 |
+
paragraph = c.paragraphs[0]
|
| 451 |
+
break
|
| 452 |
+
if not paragraph:
|
| 453 |
+
print("The index is not valid")
|
| 454 |
+
return None
|
| 455 |
+
|
| 456 |
+
table = self.xdoc.add_table(rows = len(content["rows"]) + 1, cols = len(content["headers"]))
|
| 457 |
+
#set style below
|
| 458 |
+
|
| 459 |
+
|
| 460 |
#add the header
|
| 461 |
for i, header in enumerate(content["headers"]):
|
| 462 |
table.cell(0, i).text = header
|
|
|
|
| 466 |
table.cell(i+1, j).text = cell
|
| 467 |
#insert the table after the paragraph
|
| 468 |
self.move_table_after(table, paragraph.xparagraph)
|
|
|
|
| 469 |
#save the doc
|
| 470 |
self.save_as_docx()
|
| 471 |
return table
|