Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change font size of text within a table created by Python-pptx

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)
like image 260
Jeremy Barnes Avatar asked Jul 14 '16 15:07

Jeremy Barnes


People also ask

How do I change the font size in text Python?

The first method for changing the font size is to use the set_theme function. It changes the global defaults so the adjustments will be permanent until you change them again. If you plan to use the same font size for all the plots, then this method is a highly practical one.

How do you change the font size in a table in Powerpoint?

Click the slide master to change the font on all slides, or click any of the layouts to change the font size on just that layout. On the slide master or layout, select the level of placeholder text for which you want to change the font size. On the Home tab, click the font size you want in the Font Size box.

How do you change the font size in a table?

You could add this code inline to the <table> tag, the <tr> tag or to each <th> tag. A few options but this is the code you need: style=font-size:12px, change 12 to whatever font size suits your needs. You could add this code inline to the <table> tag, the <tr> tag or to each <th> tag.

How do I change the font size in Pyscripter?

Pyscripter Tools menu > Options > Editor Options. Display tab > click Font. Select "Source Code Pro" from list of fonts. Select Style and Size if desired.


1 Answers

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.

like image 130
scanny Avatar answered Oct 20 '22 22:10

scanny