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



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

我已经能够通过将所有数字转换为字符串来插入值,如下所示。谁能建议我们是否可以直接将数字作为整数插入或浮点数到表中。

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 不同,单元格没有可以采用的数字类型。因此,"我们可以在表(单元格)中插入浮点数或整数吗"的直接答案是否定的

此外,_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)