7

I am creating a script to show graphs of product performance and creating a table to display its partnumber, a list of applications and the number of current applications.

However, the default font size is much too large to get all of this information into the slide and needs to be reduced.

How do I reduce the font size of text within a table in Python-pptx?

This is what I have, but I keep getting an error "AttributeError: '_Cell' object has no attribute 'paragraph'"

table = shapes.add_table(rows, cols, left + left_offset, top + Inches(.25), width, height - Inches(.25)).table
#column width
for i in range(3):
    table.columns[i].width = col_width[i]           
    for i in range(len(a_slide)):
        #color table
        if i % 2 == 0:
            for j in range(3):
                fill = table.cell(i, j).fill
                fill.background()
        else:
            for j in range(3):
                fill = table.cell(i, j).fill
                fill.solid()
                fill.fore_color.rgb = RGBColor(240, 128, 128)
        #populate table
        table.cell(i, 0).text = str(item["name"])
        try:
            table.cell(i, 1).text = ", ".join(item["app"])
        except:
            table.cell(i, 1).text = " "
        finally:
            table.cell(i, 2).text = str(item["vio"])
            for j in range(0,3):
                font = table.cell(i, j).paragraph[0].font
                font.size = Pt(12)
scanny
  • 26,423
  • 5
  • 54
  • 80
Jeremy Barnes
  • 642
  • 1
  • 9
  • 24

1 Answers1

8

A _Cell object doesn't directly contain paragraphs. However, it does include a TextFrame object on .text_frame which contains the paragraphs. So if you just use:

cell.text_frame.paragraphs[0]

..you should get what you expect. Note that it's .paragraphs, not .paragraph.

The API documentation for _Cell is here: http://python-pptx.readthedocs.io/en/latest/api/table.html#cell-objects

and generally provides all the details needed to resolve the finer points like this one.

scanny
  • 26,423
  • 5
  • 54
  • 80
  • Old as heck, but any chance on a slightly more elaborate clarification on what OP's done wrong? For example, do you mean he should do `font = cell.text_frame.paragraphs[0].font` and then set the size? (This is isn't working for me, hence wondering where I'm going wrong) – Hendy Sep 30 '21 at 16:49
  • Just kidding of course. Didn't re-save out my file. Sigh. For others, yes, my comment above seems to be what is implied, or at least works for me. – Hendy Sep 30 '21 at 16:53
  • @Hendy - Do you know how to get the existing font size, alignment and style from a table? To solve something like this https://stackoverflow.com/questions/73202432/python-pptx-access-table-cell-properties-and-apply-it-back-during-replace – The Great Aug 03 '22 at 01:01