本主题重点介绍如何使用 Python 在 PPTX 中删除文本。它包括建立环境的详细信息以及所需的资源、分步过程和使用 Python **删除 PPTX 文本的工作示例代码。它展示了过程细节,包括生成示例演示文稿、插入自选图形、通过插入文本和删除文本来添加文本框。
使用 Python 在 PPTX 中删除文本的步骤
- 为 通过 .NET 使用 Aspose.Slides for Python 创建环境以删除 PPTX 中的文本
- 使用 Presentation 类的实例生成默认的空演示文稿并访问其第一张幻灯片
- 创建一个自选图形并在其中插入示例文本框
- 在文本框内插入一部分文本,并使用 TextStrikethroughType 枚举器为该部分文本设置双线删除线
- 在文本框中插入第二部分文本,并使用 TextStrikethroughType 枚举器为该部分文本设置单行删除线
- 将带删除线的 PPTX 演示文稿保存在磁盘上
上述步骤展示了如何使用 Python 在 Presentation 中删除文本,方法是公开有关所有必需的类、方法和属性的详细信息以获得所需的输出。 Presentation 类用于创建一个空的或加载现有的 PPTX 文件,ShapeCollection 类用于在 PPTX 幻灯片中添加自选图形,TextStrikethroughType 枚举器用于为所选部分设置所需的文本删除线类型。
使用 Python 在 PPTX 中删除文本的代码
import aspose.pydrawing as draw | |
import aspose.slides as slides | |
# Path to the license file directory | |
filepath = "Y://Documents//KnowledgeBase//TestData//" | |
#Load the license in your application for creating a strikethrough text | |
slidesTextLicense = slides.License() | |
slidesTextLicense.set_license(filepath + "Conholdate.Total.Product.Family.lic") | |
# Instantiate the Presentation object to strikethrough text | |
with slides.Presentation() as presentationText: | |
#Access the first default slide | |
slide = presentationText.slides[0] | |
#Add an autoshape of the Rectangle type | |
autoShape = slide.shapes.add_auto_ahape(slides.ShapeType.Rectangle, 50, 150, 300, 0) | |
#Filling the shape with no fill color | |
autoShape.fill_format.fill_type = slides.FillType.NoFill | |
#Add the text frame inside the autoshape | |
textFrame = autoShape.add_text_frame("This is sample strikethrough text") | |
#Set the textual properties on the portion | |
portionFormat = textFrame.paragraphs[0].portions[0].portion_format | |
portionFormat.fill_format.fill_type = slides.FillType.Solid | |
portionFormat.fill_format.solid_fill_color.dolor = draw.Color.red | |
#Strikethrouh with double line | |
portionFormat.strikethrough_type = slides.TextStrikethroughType.Double; | |
#Add a second line of text | |
secondPortion = slides.Portion("Second text line ") | |
textFrame.Paragraphs[0].Portions.Add(secondPortion) | |
portionFormat = secondPortion.PortionFormat | |
portionFormat.fill_format.fill_type = slides.FillType.Solid | |
portionFormat.fill_format.solid_fill_color.color = draw.Color.blue | |
#Strikethrouh with a single line | |
portionFormat.strikethrough_type = slides.TextStrikethroughType.Single | |
#Save the presentation with strikethrough text on the disk | |
presentationText.save(filepath + "StrikethroughText.pptx", slides.export.SaveFormat.Pptx) | |
print("Done") |
以上示例代码使用 Python 在 PPT 中删除文本。它使用 Presentation 类实例生成默认的空演示文稿,并使用 Presentation.Slides 属性访问演示文稿幻灯片集合中的第一张幻灯片。插入自选图形,然后在其中插入部分文本。最后,在 TextStrikethroughType 枚举器的帮助下,所需的文本在演示文稿中被删除并保存在磁盘上。
在本教程中,我们学习了使用 Python* *删除演示文本。如果您有兴趣了解如何在演示文稿中合并 Islides,请参阅 如何使用 Python 合并 PowerPoint 文件 上的文章。