使用PPTX软件包将文本插入幻灯片表格时,会在所需文本之前添加换行符。

huangapple go评论76阅读模式
英文:

Inserting Text into tables of a power point slide using PPTX package adds a carriage return before desired text

问题

我正在使用Python的PPTX包将表格生成到PowerPoint演示文稿中。我有一个加载的模板,然后在第一张幻灯片上生成两个表格。

表格都是5行2列,每个表格的第一行都合并了,我试图在每个表格的合并行中输入文本。我可以插入文本,但是我的代码似乎在文本之前添加了一个换行符,然后顶部行的高度与其余行不同。

如何阻止插入换行符?我将开始填充其余表格的其他信息,所以希望在剩下的表格中也防止它发生。

英文:

I am using the Python PPTX package to generate tables into a powerpoint presentation. I have a template which is loaded then I generate two tables on the first slide.

Tables are 5,2 each with the first row merged, in the merged row of each table I am attemping to enter text. I can get the text to insert, however, my code seems to add a carriage return before the text, them the top row ends up a different height to the rest of the rows.

My code below

    from pptx.dml.color import RGBColor
    from pptx.util import Cm
    from pptx.util import Pt
    
    from pptx.enum.text import PP_ALIGN
    from pptx.dml.color import RGBColor

    prs = Presentation('pathto/Template.pptx')

    # Select the second slide (index 1 since slide indexes start from 0)
    slide = prs.slides[1]
    
    # Define the position and size of the first table
    x1, y1, cx1, cy1 = Cm(4), Cm(4), Cm(8), Cm(6)
    
    # Add the first table shape to the slide
    shape1 = slide.shapes.add_table(5, 2, x1, y1, cx1, cy1)
    
    # Access the first table object
    table1 = shape1.table
    
    # Merge cells in the top row of the first table
    table1.cell(0, 0).merge(table1.cell(0, 1))
    
    # Get the merged cell in the first table
    merged_cell1 = table1.cell(0, 0)
    
    # Set text and formatting for the merged cell in the first table
    text_frame1 = merged_cell1.text_frame
    text_frame1.clear()  # Clear existing text
    p1 = text_frame1.add_paragraph()
    p1.text = "Background Window"
    p1.alignment = PP_ALIGN.CENTER
    p1.font.name = "Calibri Light"
    p1.font.color.rgb = RGBColor(0, 0, 0)  # Black font color
    p1.font.size = Pt(14)  # Change the font size to the desired value
    
    # Define the position and size of the second table
    x2, y2, cx2, cy2 = Cm(4), Cm(11), Cm(8), Cm(6)
    
    # Add the second table shape to the slide
    shape2 = slide.shapes.add_table(5, 2, x2, y2, cx2, cy2)
    
    # Access the second table object
    table2 = shape2.table
    
    # Merge cells in the top row of the second table
    table2.cell(0, 0).merge(table2.cell(0, 1))
    
    # Get the merged cell in the second table
    merged_cell2 = table2.cell(0, 0)
    
    # Set text and formatting for the merged cell in the second table
    text_frame2 = merged_cell2.text_frame
    text_frame2.clear()  # Clear existing text
    p2 = text_frame2.add_paragraph()
    p2.text = "Activity Tracker"
    p2.alignment = PP_ALIGN.CENTER
    p2.font.name = "Calibri Light"
    p2.font.color.rgb = RGBColor(0, 0, 0)  # Black font color
    p2.font.size = Pt(14)  # Change the font size to the desired value

How do I stop the carriage return from being inserted? I am going to start populating the rest of the tables with additional information so would like to prevent it from occurring there for the remainder of the table as well.

答案1

得分: 0

我意识到我在需要引用单元格的地方使用了add.Paragraph,应更改为.text = "mytext"。

# 选择第二张幻灯片(索引为1,因为幻灯片索引从0开始)
slide = prs.slides[1]

# 定义第一个表格的位置和大小
x1, y1, cx1, cy1 = Cm(4), Cm(4), Cm(8), Cm(6)

# 将第一个表格形状添加到幻灯片
shape1 = slide.shapes.add_table(5, 2, x1, y1, cx1, cy1)

# 访问第一个表格对象
table1 = shape1.table

# 合并第一个表格的顶行单元格
table1.cell(0, 0).merge(table1.cell(0, 1))

# 获取第一个表格中合并的单元格
merged_cell1 = table1.cell(0, 0)

# 设置第一个表格中合并单元格的文本和格式
text_frame1 = merged_cell1.text_frame
text_frame1.clear()  # 清除现有文本
merged_cell1.text = "Background Window"
merged_cell1.alignment = PP_ALIGN.CENTER
merged_cell1.font.name = "Calibri Light"
merged_cell1.font.color.rgb = RGBColor(0, 0, 0)  # 黑色字体颜色
merged_cell1.font.size = Pt(14)  # 更改字体大小为所需值

# 定义第二个表格的位置和大小
x2, y2, cx2, cy2 = Cm(4), Cm(11), Cm(8), Cm(6)

# 将第二个表格形状添加到幻灯片
shape2 = slide.shapes.add_table(5, 2, x2, y2, cx2, cy2)

# 访问第二个表格对象
table2 = shape2.table

# 合并第二个表格的顶行单元格
table2.cell(0, 0).merge(table2.cell(0, 1))

# 获取第二个表格中合并的单元格
merged_cell2 = table2.cell(0, 0)

# 设置第二个表格中合并单元格的文本和格式
text_frame2 = merged_cell2.text_frame
text_frame2.clear()  # 清除现有文本

merged_cell2.text = "Activity Tracker"
merged_cell2.alignment = PP_ALIGN.CENTER
merged_cell2.font.name = "Calibri Light"
merged_cell2.font.color.rgb = RGBColor(0, 0, 0)  # 黑色字体颜色
merged_cell2.font.size = Pt(14)  # 更改字体大小为所需值
英文:

I realized I am using add.Paragraph where I needed reference the cell with .text = "mytext"


# Select the second slide (index 1 since slide indexes start from 0)
slide = prs.slides[1]
# Define the position and size of the first table
x1, y1, cx1, cy1 = Cm(4), Cm(4), Cm(8), Cm(6)
# Add the first table shape to the slide
shape1 = slide.shapes.add_table(5, 2, x1, y1, cx1, cy1)
# Access the first table object
table1 = shape1.table
# Merge cells in the top row of the first table
table1.cell(0, 0).merge(table1.cell(0, 1))
# Get the merged cell in the first table
merged_cell1 = table1.cell(0, 0)
# Set text and formatting for the merged cell in the first table
text_frame1 = merged_cell1.text_frame
text_frame1.clear()  # Clear existing text
merged_cell1.text = "Background Window"
merged_cell1.alignment = PP_ALIGN.CENTER
merged_cell1.font.name = "Calibri Light"
merged_cell1.font.color.rgb = RGBColor(0, 0, 0)  # Black font color
merged_cell1.font.size = Pt(14)  # Change the font size to the desired value
# Define the position and size of the second table
x2, y2, cx2, cy2 = Cm(4), Cm(11), Cm(8), Cm(6)
# Add the second table shape to the slide
shape2 = slide.shapes.add_table(5, 2, x2, y2, cx2, cy2)
# Access the second table object
table2 = shape2.table
# Merge cells in the top row of the second table
table2.cell(0, 0).merge(table2.cell(0, 1))
# Get the merged cell in the second table
merged_cell2 = table2.cell(0, 0)
# Set text and formatting for the merged cell in the second table
text_frame2 = merged_cell2.text_frame
text_frame2.clear()  # Clear existing text
merged_cell2.text = "Activity Tracker"
merged_cell2.alignment = PP_ALIGN.CENTER
merged_cell2.font.name = "Calibri Light"
merged_cell2.font.color.rgb = RGBColor(0, 0, 0)  # Black font color
merged_cell2.font.size = Pt(14)  # Change the font size to the desired value

huangapple
  • 本文由 发表于 2023年6月19日 17:37:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76505383.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定