Skip to content

Set_cell_border #1088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
asmlgkj opened this issue May 27, 2022 · 5 comments
Closed

Set_cell_border #1088

asmlgkj opened this issue May 27, 2022 · 5 comments

Comments

@asmlgkj
Copy link

asmlgkj commented May 27, 2022

thanks a lot for your great work in this package, very useful.
I want to set border for a table like such
image

I want to set border for each cell, and some cell has no rigth frame.

I found some kind people offered a function like such

from docx.oxml import OxmlElement
from docx.oxml.ns import qn

def set_cell_border(cell: _Cell, **kwargs):
    """
    Set cell`s border
    Usage:

    set_cell_border(
        cell,
        top={"sz": 12, "val": "single", "color": "#FF0000", "space": "0"},
        bottom={"sz": 12, "color": "#00FF00", "val": "single"},
        start={"sz": 24, "val": "dashed", "shadow": "true"},
        end={"sz": 12, "val": "dashed"},
    )
    """
    tc = cell._tc
    tcPr = tc.get_or_add_tcPr()

    # check for tag existnace, if none found, then create one
    tcBorders = tcPr.first_child_found_in("w:tcBorders")
    if tcBorders is None:
        tcBorders = OxmlElement('w:tcBorders')
        tcPr.append(tcBorders)

    # list over all available tags
    for edge in ('start', 'top', 'end', 'bottom', 'insideH', 'insideV'):
        edge_data = kwargs.get(edge)
        if edge_data:
            tag = 'w:{}'.format(edge)

            # check for tag existnace, if none found, then create one
            element = tcBorders.find(qn(tag))
            if element is None:
                element = OxmlElement(tag)
                tcBorders.append(element)

            # looks like order of attributes is important
            for key in ["sz", "val", "color", "space", "shadow"]:
                if key in edge_data:
                    element.set(qn('w:{}'.format(key)), str(edge_data[key]))

but I do not know how to set the right frame as blank, and set the space between column 2 and 3

@asmlgkj
Copy link
Author

asmlgkj commented May 27, 2022

I also want to know is there a table style list
I refered to #149
table.style = 'Light List Accent 1'
but still gives error KeyError: "no style with name 'Light List Accent 1'"

@asmlgkj
Copy link
Author

asmlgkj commented May 27, 2022

document = Document()
styles = document.styles
table_styles = [
s for s in styles if s.type == WD_STYLE_TYPE.TABLE
]
for style in table_styles:
print(style.name)

output

Normal Table
Table Grid
Light Shading
Light Shading Accent 1
Light Shading Accent 2
Light Shading Accent 3
Light Shading Accent 4
Light Shading Accent 5
Light Shading Accent 6
Light List
Light List Accent 1
Light List Accent 2
Light List Accent 3
Light List Accent 4
Light List Accent 5
Light List Accent 6
Light Grid
Light Grid Accent 1
Light Grid Accent 2
Light Grid Accent 3
Light Grid Accent 4
Light Grid Accent 5
Light Grid Accent 6
Medium Shading 1
Medium Shading 1 Accent 1
Medium Shading 1 Accent 2
Medium Shading 1 Accent 3
Medium Shading 1 Accent 4
Medium Shading 1 Accent 5
Medium Shading 1 Accent 6
Medium Shading 2
Medium Shading 2 Accent 1
Medium Shading 2 Accent 2
Medium Shading 2 Accent 3
Medium Shading 2 Accent 4
Medium Shading 2 Accent 5
Medium Shading 2 Accent 6
Medium List 1
Medium List 1 Accent 1
Medium List 1 Accent 2
Medium List 1 Accent 3
Medium List 1 Accent 4
Medium List 1 Accent 5
Medium List 1 Accent 6
Medium List 2
Medium List 2 Accent 1
Medium List 2 Accent 2
Medium List 2 Accent 3
Medium List 2 Accent 4
Medium List 2 Accent 5
Medium List 2 Accent 6
Medium Grid 1
Medium Grid 1 Accent 1
Medium Grid 1 Accent 2
Medium Grid 1 Accent 3
Medium Grid 1 Accent 4
Medium Grid 1 Accent 5
Medium Grid 1 Accent 6
Medium Grid 2
Medium Grid 2 Accent 1
Medium Grid 2 Accent 2
Medium Grid 2 Accent 3
Medium Grid 2 Accent 4
Medium Grid 2 Accent 5
Medium Grid 2 Accent 6
Medium Grid 3
Medium Grid 3 Accent 1
Medium Grid 3 Accent 2
Medium Grid 3 Accent 3
Medium Grid 3 Accent 4
Medium Grid 3 Accent 5
Medium Grid 3 Accent 6
Dark List
Dark List Accent 1
Dark List Accent 2
Dark List Accent 3
Dark List Accent 4
Dark List Accent 5
Dark List Accent 6
Colorful Shading
Colorful Shading Accent 1
Colorful Shading Accent 2
Colorful Shading Accent 3
Colorful Shading Accent 4
Colorful Shading Accent 5
Colorful Shading Accent 6
Colorful List
Colorful List Accent 1
Colorful List Accent 2
Colorful List Accent 3
Colorful List Accent 4
Colorful List Accent 5
Colorful List Accent 6
Colorful Grid
Colorful Grid Accent 1
Colorful Grid Accent 2
Colorful Grid Accent 3
Colorful Grid Accent 4
Colorful Grid Accent 5
Colorful Grid Accent 6

@asmlgkj
Copy link
Author

asmlgkj commented May 27, 2022

I used my own docx

Normal Table
Table Grid
Table Normal
Grid Table 1 Light Accent 5
Grid Table 4 Accent 5
Grid Table 3 Accent 5
Grid Table 2 Accent 5
hot gene
mutation result
Plain Table 3
mutation result6
网格表 4 - 着色 61
baseinfo
mutation result1
baseinfo11
mutation result11
mutation result5
smartonco11
网格表 4 - 着色 51
smartonco111
网格表 4 - 着色 52
网格表 4 - 着色 611
mutation result31
Grid Table 6 Colorful Accent 5
清单表 2 - 着色 61
样式1
样式2
网格表 1 浅色 - 着色 52
网格型2
网格表 1 浅色 - 着色 51
网格表 3 - 着色 51
网格表 2 - 着色 51
无格式表格 31
网格表 6 彩色 - 着色 51
网格表 1 浅色 - 着色 53
网格表 1 浅色 - 着色 511
网格表 1 浅色 - 着色 512
网格表 1 浅色 - 着色 513
网格型1
样式11

@asmlgkj
Copy link
Author

asmlgkj commented May 27, 2022

how can I used all the styles? thanks a lot

@asmlgkj
Copy link
Author

asmlgkj commented May 30, 2022

solved
but the run time is long for inserting a 100 rows * 7 columns table, is there any measures to speed up

@asmlgkj asmlgkj closed this as completed May 31, 2022
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

No branches or pull requests

1 participant