Skip to content
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

feature: cell border #71

Open
scanny opened this issue Dec 16, 2013 · 12 comments
Open

feature: cell border #71

scanny opened this issue Dec 16, 2013 · 12 comments
Labels
Milestone

Comments

@scanny
Copy link
Owner

scanny commented Dec 16, 2013

set borders of table cell; left, right, top, and bottom

Attributes: color (fill), thickness (weight), style (single, thick-thin, thin-thin, etc.), inset pen (inside boundary or centered on it), line join (round, bevel, miter), and line cap (round, square, flat).

@scanny scanny added this to the v0.4.0 milestone Mar 30, 2014
@scanny scanny added the table label Jun 15, 2014
@scanny scanny modified the milestones: v0.4.0, later Nov 16, 2014
@glayman
Copy link

glayman commented Mar 26, 2015

Hi, are you still planning to implement this feature? If not, is there a way that I can incorporate this functionality until a feature is built? For now, I need the ability to draw horizontal borders across multiple cells in a table, and set the color, weight and style.

@smcpherson
Copy link

👍 for this feature request.

@rpeys
Copy link

rpeys commented Jul 13, 2017

I would benefit from this feature as well! If it won't be implemented for a while, have any hints for how to access this xml feature?

@dereitz
Copy link

dereitz commented Dec 12, 2017

Are there any workarounds to this? I've run into a scenario where I'd like to change the border in a table. Can the default be changed such that no borders are introduced in a table (as a workaround)?

@dereitz
Copy link

dereitz commented Dec 13, 2017

I found this thread. One thing to note is that order appears to be important. In my case, the lnB element had to come before the solidFill element!

https://groups.google.com/forum/#!topic/python-pptx/UTkdemIZICw

@ahuang11
Copy link

ahuang11 commented Feb 10, 2021

A reproducible example; as dereitz mentioned, make sure you set the cell.fill.solid() after borders!

import pptx
from pptx import Presentation
from pptx.util import Inches
from pptx.oxml.xmlchemy import OxmlElement

def SubElement(parent, tagname, **kwargs):
    element = OxmlElement(tagname)
    element.attrib.update(kwargs)
    parent.append(element)
    return element


def _set_cell_border(cell, border_color="000000", border_width='12700'):
    """ Hack function to enable the setting of border width and border color
        - bottom border only at present
        (c) Steve Canny
    """
    tc = cell._tc
    tcPr = tc.get_or_add_tcPr()

    lnR = SubElement(
        tcPr, 'a:lnR', w=border_width, cap='flat', cmpd='sng', algn='ctr')
    solidFill = SubElement(lnR, 'a:solidFill')
    srgbClr = SubElement(solidFill, 'a:srgbClr', val=border_color)
    return cell



prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[5])
placeholder = slide.placeholders[0]
x, y, cx, cy = Inches(2), Inches(2), Inches(4), Inches(1.5)
shape = slide.shapes.add_table(3, 3, x, y, cx, cy)
table = shape.table

for r in range(3):
    for c in range(3):
        cell = table.cell(r, c)
        cell = _set_cell_border(cell)
        cell.fill.solid()
        cell.fill.fore_color.rgb = pptx.dml.color.RGBColor(255, 0, 0)
        if c == 1:
            break
prs.save('test.pptx')

Should get something like
image

@MartinPacker
Copy link

If this were ever to make it into the python-pptx code I'd also like to enable / disable table borders. I see the <tblborder> element in the spec. (See here.)

For md2pptx I will probably roll my own.

It just seems to me table and cell borders really would be a consistent thing to work on together.

@MartinPacker
Copy link

Actually that link in my previous only works for Excel. Hex dumping a table with a border doesn't reveal how it does it.

@scanny
Copy link
Owner Author

scanny commented Jul 24, 2021

@MartinPacker judging from how the PowerPoint UI allows borders to be applied I'd say that's on a cell-by-cell basis, like to put a border around a table you have to visit each perimeter cell and set its outward-facing borders to the desired value.

If I select the table and click on "Table Design > Borders > Outside Borders" then PowerPoint applies those individually to each perimeter cell. Interestingly, if I then add a new row at the bottom by tabbing through the last cell, it carries those borders down in the expected way. So it must have some "border-moving" logic in there on growing or shrinking the table.

It's possible such a thing can be set using a customized table style but it's not immediately clear to me how after a look at the schema. I expect there is a series of experiments between here and knowing with confidence what can be managed there.

@MartinPacker
Copy link

MartinPacker commented Jul 24, 2021

Right. That's what I gathered. And that implies one of two approaches:

  1. Apply the border after all the cells have been created.
  2. Adjust the border settings on adding cells.

If I do this I think I can manage to do Option 1 in md2pptx - as I know the numbers of rows and columns when creating the table. But I can see in general this wouldn't be fun for python-pptx to do.

Also I notice the snippet @ahuang11 posted appears to have a copyright notice of yours. Does that mean this code isn't open source? If so I will re-implement by adding elements / attributes individually. (Actually I should probably do that for flexibility.)

I ask about Copyright as I want md2pptx to remain MIT licensed.

@ahuang11
Copy link

I found that code here so I'm not sure:
https://groups.google.com/g/python-pptx/c/UTkdemIZICw

@scanny
Copy link
Owner Author

scanny commented Jul 25, 2021

No, there's no copyright. @ahuang11 Just a URL to the mailing-list source is fine if you want to add a citation, but I'm not particular about that. Best not to add a copyright though. Anything I post here or on SO or a mailing-list is public domain as far as I'm concerned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants