3

I am trying to extract table from a PPT using python-pptx, however, the I am not sure how do I that using shape.table.

from pptx import Presentation
prs = Presentation(path_to_presentation)
# text_runs will be populated with a list of strings,
# one for each text run in presentation
text_runs = []
for slide in prs.slides:
  for shape in slide.shapes:
    if shape.has_table:
      tbl = shape.table
      rows = tbl.rows.count
      cols = tbl.columns.count

I found a post here but the accepted solution does not work, giving error that count attribute is not available.

How do I modify the above code so I can get a table in a dataframe?

EDIT

Please see the image of the slide below

enter image description here

chintan s
  • 6,170
  • 16
  • 53
  • 86

2 Answers2

5

This appears to work for me.


prs = Presentation((path_to_presentation))
# text_runs will be populated with a list of strings,
# one for each text run in presentation
text_runs = []
for slide in prs.slides:
    for shape in slide.shapes:
        if not shape.has_table:
            continue    
        tbl = shape.table
        row_count = len(tbl.rows)
        col_count = len(tbl.columns)
        for r in range(0, row_count):
            for c in range(0, col_count):
                cell = tbl.cell(r,c)
                paragraphs = cell.text_frame.paragraphs 
                for paragraph in paragraphs:
                    for run in paragraph.runs:
                        text_runs.append(run.text)

print(text_runs)```





0

To read the values present inside ppt | This code worked for me

slide = Deck.slides[1]
table = slide.shapes[1].table
for r in range(0,len(table.rows)):
    for c in range(2,len(table.columns)):
        cell_value = (table.cell(r,c)).text_frame.text
        print(cell_value)