Skip to content

Feature table enhancements#1129

Open
yudytskiy wants to merge 2 commits intopython-openxml:masterfrom
yudytskiy:feature_table_enhancements
Open

Feature table enhancements#1129
yudytskiy wants to merge 2 commits intopython-openxml:masterfrom
yudytskiy:feature_table_enhancements

Conversation

@yudytskiy
Copy link
Copy Markdown

@yudytskiy yudytskiy commented Aug 12, 2022

Hi.
I've made some changes to Table and _Cell proxy classes as well as oxml classes templates.
This patch adds borders property to the Table and _Cell classes that could be used to get access to borders, add and remove borders of table and table cell.
In addition table _Row class has dont_split property that implements CT_TrPr w:cantSplit tag
I have not added any tests to the project, sorry for that.

Definitely my code might be optimized and refactored, but I have tried to keep along with current code style and base structure.

Code examples:
Table borders

from docx import Document
doc = Document()
tab = doc.add_table(rows=0, cols=2)
tab.autofit = True
for side in ('top', 'bottom', 'start', 'end'):
    tab.borders.add_border(side, 'single')
tab.borders.add_border('insideV', 'double')
# Change line style for the top border.
# One can change folowing properties of border instance:
# name = 'top' | 'bottom' | 'start' | 'end' |  'insideV' | 'insideH'
# causes removing existing border with old name and creating copy of the border with new name
# line: 'single' | 'double' and many other types of line documented in <xsd:simpleType name="ST_Shd"> 
# size: unsigned decimal ST_EighthPointMeasure
# space: unsigned decimal  ST_PointMeasure
# color:  'auto' and others corresponding to ST_HexColor
tab.borders[0].line = 'double'  
row = tab.add_row()
# Table row now has dont_split property to avoid splitting paragraphs and runs of a row when page break accurs
row.dont_split = True
row.cells[0].text = 'Cell1'
doc.save('test_border.docx')

Table cell borders

from docx import Document
doc = Document()
tab = doc.add_table(rows=0, cols=2)
tab.autofit = True
row = tab.add_row()
cell = row.cells[0]
cell.text = 'Cell1'
for side in ('top', 'bottom', 'start', 'end'):
    cell.borders.add_border(side, 'single')
cell.borders.add_border('tl2br',  'double')
# One can change folowing properties of border instance:
# name = 'top' | 'start' | 'left' | 'bottom' | 'end' | 'right' | 'insideH' | 'insideV' | 'tl2br' | 'tr2bl'
cell.borders[0].line = 'double' 
doc.save('test_border.docx')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant