Border Conflicts
This chapter shows a common problem and its solution.
Border Conflicts Exercise
Open border_conflicts.docx in the DocxFactory/exercises/templates/ directory, open the Bookmarks dialog box and highlight the Line item (see picture below).
The problem is that the Line item shares a border with the report summary and there is no separate bottom border for the Line item and a separate top border for the report summary, to set with different border styles.
To fix this problem create a dummy item between them. The dummy item is not pasted and is only used to separate the items. After the dummy item is added there is a separate bottom border for the Line item and a separate top border for the report summary that can be set with different border styles.
When creating a new document and pasting items, if items meet on the same border with different border styles then, in general the bigger border will overtake the smaller border (the thicker border, the border with the most lines etc.).
First, insert a new row between the items.
Place the cursor on the bottom of the Date column and select Insert Below in the Layout ribbon (see picture below).
Then change the Line item bottom border.
Highlight the Line item, right click the highlighted area and select Borders and Shading from the drop down menu. In the Borders and Shading dialog box, change the bottom border to a single line (see picture below).
Finally, create a bookmark for the dummy item (see picture below).
Dummy items are not pasted or referenced and there is no need to give them meaningful names. Dummy items are named “Dummy<n>” by convention.
Compile the template.
Create the .DOCX file.
Copy and run the code below.
from docxfactory import WordProcessingMerger from datetime import datetime try: merger = WordProcessingMerger.get_instance() start_time = datetime.now() merger.load("/opt/DocxFactory/exercises/templates/border_conflicts.dfw") merger.paste("Account") for i in range(3): merger.paste("Line") merger.save("/tmp/border_conflicts.docx") print("Completed (in", round((datetime.now() - start_time).total_seconds(), 3), "seconds).") exception Exception as e: print(str(e)) |
Open the created .DOCX file.
As you can see, the Line item bottom border has a different border style than the report summary top border style and on the border where the Line item and the report summary meet the report summary overtakes the Line item border style (see picture below).