0

I am trying to insert some numbers into a powerpoint deck using the python-pptx module.

I have been able to insert values by converting all numbers to string as shown below. Could anyone advice if we can directly insert a number as an integer or float into the table.

slide = prs.slides.add_slide(prs.slide_layouts[1])
shapes = slide.shapes

shapes.title.text = 'Summary'

rows = 3
cols = 2
left = Inches(2.0)
top = Inches(2.0)
width = Inches(6.0)
height = Inches(0.8)

table = shapes.add_table(rows, cols, left, top, width, height).table

# set column widths
table.columns[0].width = Inches(4.0)
table.columns[1].width = Inches(4.0)

# write column headings
table.cell(0, 0).text = 'Name'
table.cell(0, 1).text = 'Value'

# write body cells
table.cell(1, 0).text = 'Total Sales'
table.cell(1, 1).text = str(count) <<- Want to insert the value of count directly without having to convert to a string
hello kee
  • 289
  • 2
  • 6
  • 17

1 Answers1

3

PowerPoint tables are essentially string-oriented. Unlike Excel, there is no numeric type a cell can take on. So the direct answer to "can we insert a float or integer into a table (cell)" is no.

Further, _Cell.text in python-pptx accepts only a string; it will not do automatic type conversion for you.

If you wanted something like that you could extract it as a private function, perhaps something like:

def write_cell(table, row, col, value):
    table.cell(row, col).text = "%s" % value

write_cell(table, 1, 0, "Total Sales")
write_cell(table, 1, 1, count)
scanny
  • 26,423
  • 5
  • 54
  • 80