python-pptx -- 尝试将数字插入 table 中作为整数类型而不是字符串

python-pptx -- Trying to insert a number into a table as type integer and not string

我正在尝试使用 python-pptx 模块将一些数字插入到 powerpoint 幻灯片中。

我已经能够通过将所有数字转换为字符串来插入值,如下所示。谁能建议我们是否可以直接将数字作为整数或浮点数插入 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

PowerPoint 表格本质上是面向字符串的。与 Excel 不同,单元格没有可以采用的数字类型。所以 "can we insert a float or integer into a table (cell)" 的直接答案是 no.

此外,_Cell.text in python-pptx 只接受一个字符串;它不会为你做自动类型转换。

如果你想要这样的东西,你可以将它提取为一个私有函数,也许是这样的:

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)