3

I have a dataframe that looks like this:

    c      sp    k1    k2    k3    k4    k5    k6
0  c1   70.73  0.3%  0.6%  0.7%  0.8%  0.7%  0.5%
1  c2  149.71  0.7%  0.6%  0.4%  0.6%  0.7%  1.0%
2  c3   -1.00  0.0%  0.0%  0.0%  0.0%  0.0%  0.0%
3  c4   24.88  0.1%  0.9%  0.5%  0.7%  0.7%  0.9%
4  c5  276.23  0.3%  2.3%  0.4%  2.0%  1.9%  1.9%

I am creating a slide in a ppt for this table by using this code:

prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[0])
title = slide.shapes.title
title.text = "Title"
title.top = Cm(1) # set title position
title.left = Cm(1) # set title position
title.width = Cm(24) # set title size
title.height = Cm(2) # set title size
top = Inches(1.5) # set table position
left = Inches(0.25) # set table position
width = Inches(9.25) # set table size
height = Inches(5.0) # set table size

tbl1 = df_to_table(slide, dt, left, top, width, height, name='tbl1')

# changing the font size and font of the whole table
for cell in iter_cells(tbl1.table):
        for paragraph in cell.text_frame.paragraphs:
            for run in paragraph.runs:
                run.font.size = Pt(12)
                run.font.name = 'Calibri'

prs.save('test.pptx')

I am struggling in applied the following changes in this table though

1- I would like to add thick top and bottom border at the column names

2- Right border at the column sp

3- Make red color the font color of all the cs that have negative sp

4- Apply green color formatting in all the cells in k columns so that the higher the value the darker the green of the cell

5- Adjust the size of the cells, so that the text of the cell autofits the cell

I am not really sure if all these formatting changes are possible using the pptx package

Note: The df_to_table function comes from here

quant
  • 4,062
  • 5
  • 29
  • 70
  • 1
    From that table tool source: "The table is a standard Powerpoint table, and can easily be modified with the Powerpoint tools, for example: resizing columns, changing formatting etc." which begs the question [did you try that](https://python-pptx.readthedocs.io/en/latest/user/table.html)? – Jongware Feb 06 '20 at 13:22
  • @usr2564301 I found this guide as well, but I didnt find there how to do any of the formatting I mention in the question. Unless I am missing something – quant Feb 06 '20 at 13:43

1 Answers1

0

1+2 ... Do you know how to do this in PowerPoint? If so, you could compare two files with and without thick border to find which part of the xml-file has to be changed.

3+4 ... you have to change the font color of the corresponding paragraphs. You could do this directly via python-pptx or you could use a font style (python-pptx-interface) and "write" it to paragraphs.

5 ... autofitting is not working. You would need to calculate the size yourself. With a table style you could at least define a ratio for the column sizes.

natter1
  • 354
  • 2
  • 10