Python aracılığıyla Java ile PowerPoint Metin Paragraflarını Yönetme
Genel Bakış
Aspose.Slides for Python via Java, metni metin çerçeveleri, paragraflar ve bölümler hiyerarşisi olarak temsil eder:
- TextFrame bir şekildeki metin kapsayıcısını temsil eder ve paragraf koleksiyonuna erişim sağlar.
- Paragraph bir metin çerçevesindeki bir paragrafı temsil eder ve bölümlerine ve paragraf düzeyinde biçimlendirmeye erişim sağlar.
- Portion bir paragraftaki metin akışını temsil eder. Her bölüm kendi metnine ve karakter düzeyinde biçimlendirmeye sahip olabilir.
Bu nedenle bir paragraf, birden çok bölüm kullanarak farklı yazı tipleri, renkler, boyutlar ve diğer biçimlendirmeler içerebilir.
Paragrafları Oluşturma ve Biçimlendirme
Bir şeklin metin çerçevesinde paragraflar oluşturun ve bölümleri kullanarak tek tek metin akışlarını biçimlendirin. Bu bölüm, her paragrafta birden çok bölümü birleştirerek farklı renkler, yazı tipi boyutları ve stiller uygulamayı gösterir.
Birden Çok Bölüm ile Paragraflar Oluşturma
Aşağıdaki Python örneği, Paragraph.getPortions tarafından döndürülen koleksiyonda üç paragraf oluşturur; her biri üç metin akışı içerir. Varsayılan paragraf ve boş bölümü yeniden kullanır, ardından kalan paragrafları ve bölümleri ekler. Biçimlendirme, Portion.getPortionFormat aracılığıyla uygulanır: her paragraftaki ilk bölüm kırmızı, kalın ve 15 puan; ikinci bölüm mavi, italik ve 18 puan. Üçüncü bölüm varsayılan biçimini korur:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import FillType, NullableBool, Paragraph, Portion, Presentation, SaveFormat, ShapeType
from java.awt import Color
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 150, 300, 150)
text_frame = shape.getTextFrame()
first_paragraph = text_frame.getParagraphs().get_Item(0)
first_paragraph.getPortions().add(Portion())
first_paragraph.getPortions().add(Portion())
second_paragraph = Paragraph()
second_paragraph.getPortions().add(Portion())
second_paragraph.getPortions().add(Portion())
second_paragraph.getPortions().add(Portion())
text_frame.getParagraphs().add(second_paragraph)
third_paragraph = Paragraph()
third_paragraph.getPortions().add(Portion())
third_paragraph.getPortions().add(Portion())
third_paragraph.getPortions().add(Portion())
text_frame.getParagraphs().add(third_paragraph)
paragraph_count = text_frame.getParagraphs().getCount()
for paragraph_index in range(paragraph_count):
paragraph = text_frame.getParagraphs().get_Item(paragraph_index)
portion_count = paragraph.getPortions().getCount()
for portion_index in range(portion_count):
portion = paragraph.getPortions().get_Item(portion_index)
portion.setText(f"Portion {paragraph_index + 1}.{portion_index + 1}")
if portion_index == 0:
portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid)
portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.RED)
portion.getPortionFormat().setFontBold(NullableBool.True_)
portion.getPortionFormat().setFontHeight(15)
elif portion_index == 1:
portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid)
portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLUE)
portion.getPortionFormat().setFontItalic(NullableBool.True_)
portion.getPortionFormat().setFontHeight(18)
presentation.save("paragraphs_with_portions.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Madde İşaretli ve Numaralı Listeler Oluşturma
Paragraf metnini semboller, sayılar veya özel görseller içeren listelere organize edin. Bu bölümdeki örnekler madde işareti biçimlendirmesini, çok katmanlı listeleri ve özel başlangıç sayısını kapsar.
Madde İşareti veya Numaralı Liste Oluşturma
Madde işaretleri ve numaralar ilgili öğelerin taranmasını kolaylaştırır. Aspose.Slides içinde liste ayarları BulletFormat aracılığıyla tanımlanır.
Bu Python örneği aynı metin çerçevesine bir sembol madde işareti ve bir numaralı öğe ekler. BulletFormat.setType madde işareti tipini seçerken, BulletFormat.setChar sembolü sağlar ve BulletFormat.setNumberedBulletStyle numaralandırma stilini seçer. Her iki öğe de %100 yükseklikte siyah madde işaretleri ve 25 puan paragraf girintisi kullanır:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import BulletType, ColorType, NullableBool, NumberedBulletStyle, Paragraph, Presentation, SaveFormat, ShapeType
from java.awt import Color
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200)
text_frame = shape.getTextFrame()
text_frame.getParagraphs().clear()
symbol_paragraph = Paragraph()
symbol_paragraph.setText("Welcome to Aspose.Slides")
symbol_paragraph.getParagraphFormat().getBullet().setType(BulletType.Symbol)
symbol_paragraph.getParagraphFormat().getBullet().setChar("•")
symbol_paragraph.getParagraphFormat().setIndent(25)
symbol_paragraph.getParagraphFormat().getBullet().getColor().setColorType(ColorType.RGB)
symbol_paragraph.getParagraphFormat().getBullet().getColor().setColor(Color.BLACK)
symbol_paragraph.getParagraphFormat().getBullet().setBulletHardColor(NullableBool.True_)
symbol_paragraph.getParagraphFormat().getBullet().setHeight(100)
text_frame.getParagraphs().add(symbol_paragraph)
numbered_paragraph = Paragraph()
numbered_paragraph.setText("This is a numbered item")
numbered_paragraph.getParagraphFormat().getBullet().setType(BulletType.Numbered)
numbered_paragraph.getParagraphFormat().getBullet().setNumberedBulletStyle(NumberedBulletStyle.BulletCircleNumWDBlackPlain)
numbered_paragraph.getParagraphFormat().setIndent(25)
numbered_paragraph.getParagraphFormat().getBullet().getColor().setColorType(ColorType.RGB)
numbered_paragraph.getParagraphFormat().getBullet().getColor().setColor(Color.BLACK)
numbered_paragraph.getParagraphFormat().getBullet().setBulletHardColor(NullableBool.True_)
numbered_paragraph.getParagraphFormat().getBullet().setHeight(100)
text_frame.getParagraphs().add(numbered_paragraph)
presentation.save("bulleted_and_numbered_list.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Resimli Madde İşaretleri Kullanma
Resimli madde işaretleri, bir sembol veya sayı yerine özel bir görsel kullanmanızı sağlar.
Bu Python örneği bir PPImage olarak sunumun görsel koleksiyonuna bir görsel ekler ve bunu paragraf madde işareti olarak kullanır. BulletFormat.setType ile resimli madde işareti seçilir ve görsel BulletFormat.getPicture ile atanır. BulletFormat.setHeight %100 olarak ayarlanır, böylece madde işareti paragraf metnine göre boyutlandırılır:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import BulletType, Images, Paragraph, Presentation, SaveFormat, ShapeType
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
bullet_image = Images.fromFile("bullets.png")
try:
presentation_image = presentation.getImages().addImage(bullet_image)
finally:
bullet_image.dispose()
shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200)
text_frame = shape.getTextFrame()
text_frame.getParagraphs().clear()
paragraph = Paragraph()
paragraph.setText("Welcome to Aspose.Slides")
paragraph.getParagraphFormat().getBullet().setType(BulletType.Picture)
paragraph.getParagraphFormat().getBullet().getPicture().setImage(presentation_image)
paragraph.getParagraphFormat().getBullet().setHeight(100)
text_frame.getParagraphs().add(paragraph)
presentation.save("picture_bullet.pptx", SaveFormat.Pptx)
presentation.save("picture_bullet.ppt", SaveFormat.Ppt)
finally:
presentation.dispose()
Çok Katmanlı Liste Oluşturma
ParagraphFormat.setDepth ayarlanarak paragraflar bir listenin farklı seviyelerinde konumlandırılır. Üst seviye derinliği 0 dır.
Bu Python örneği dört paragraf için ParagraphFormat.setDepth değerlerini 0’dan 3’e kadar atar ve tek bir metin çerçevesi içinde hiyerarşi oluşturur. Her paragraf bir sembol madde işareti kullanır; BulletFormat.setChar bu seviyeleri ayırmak için yuvarlak madde işareti ve tire arasında değişir:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import BulletType, FillType, Paragraph, Presentation, SaveFormat, ShapeType
from java.awt import Color
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200)
text_frame = shape.getTextFrame()
text_frame.getParagraphs().clear()
first_paragraph = Paragraph()
first_paragraph.setText("Content")
first_paragraph.getParagraphFormat().getBullet().setType(BulletType.Symbol)
first_paragraph.getParagraphFormat().getBullet().setChar("•")
first_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid)
first_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
first_paragraph.getParagraphFormat().setDepth(0)
second_paragraph = Paragraph()
second_paragraph.setText("Second level")
second_paragraph.getParagraphFormat().getBullet().setType(BulletType.Symbol)
second_paragraph.getParagraphFormat().getBullet().setChar('-')
second_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid)
second_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
second_paragraph.getParagraphFormat().setDepth(1)
third_paragraph = Paragraph()
third_paragraph.setText("Third level")
third_paragraph.getParagraphFormat().getBullet().setType(BulletType.Symbol)
third_paragraph.getParagraphFormat().getBullet().setChar("•")
third_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid)
third_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
third_paragraph.getParagraphFormat().setDepth(2)
fourth_paragraph = Paragraph()
fourth_paragraph.setText("Fourth level")
fourth_paragraph.getParagraphFormat().getBullet().setType(BulletType.Symbol)
fourth_paragraph.getParagraphFormat().getBullet().setChar('-')
fourth_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid)
fourth_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
fourth_paragraph.getParagraphFormat().setDepth(3)
text_frame.getParagraphs().add(first_paragraph)
text_frame.getParagraphs().add(second_paragraph)
text_frame.getParagraphs().add(third_paragraph)
text_frame.getParagraphs().add(fourth_paragraph)
presentation.save("multilevel_list.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Numaralı Liste Öğelerini Özel Değerlerle Başlatma
BulletFormat.setNumberedBulletStartWith kullanılarak numaralı bir paragraf için gösterilecek ilk sayı ayarlanabilir.
Bu Python örneği üç paragraf için numaralandırma kullanmak üzere BulletFormat.setType ayarlar ve BulletFormat.setNumberedBulletStartWith değerlerini 2, 3 ve 7 olarak atar. Başlangıç değeri her paragraf için ayrı ayrı yapılandırılır, böylece gösterilen sayılar atlanabilir:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import BulletType, Paragraph, Presentation, SaveFormat, ShapeType
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200)
text_frame = shape.getTextFrame()
text_frame.getParagraphs().clear()
first_paragraph = Paragraph()
first_paragraph.setText("Start at 2")
first_paragraph.getParagraphFormat().getBullet().setType(BulletType.Numbered)
first_paragraph.getParagraphFormat().getBullet().setNumberedBulletStartWith(2)
text_frame.getParagraphs().add(first_paragraph)
second_paragraph = Paragraph()
second_paragraph.setText("Start at 3")
second_paragraph.getParagraphFormat().getBullet().setType(BulletType.Numbered)
second_paragraph.getParagraphFormat().getBullet().setNumberedBulletStartWith(3)
text_frame.getParagraphs().add(second_paragraph)
third_paragraph = Paragraph()
third_paragraph.setText("Start at 7")
third_paragraph.getParagraphFormat().getBullet().setType(BulletType.Numbered)
third_paragraph.getParagraphFormat().getBullet().setNumberedBulletStartWith(7)
text_frame.getParagraphs().add(third_paragraph)
presentation.save("custom_numbered_list.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Paragraf Düzeni ve Bitiş Özelliklerini Kontrol Etme
Paragraf kenar boşluklarını ve girintilerini ayarlayarak ilk satır ve satır başı konumlarını denetleyin. Paragraf son işaretini görünür metin bölümlerinden ayrı biçimlendirebilirsiniz.
İlk Satır Girintisi Ayarlama
ParagraphFormat.setIndent yöntemi, bir paragrafın ilk satır girintisini kontrol eder. Bu ayar yalnızca ilk satırı paragrafın sol kenar boşluğuna göre hareket ettirir. Pozitif bir değer ilk satırı sağa kaydırırken kalan satırlar paragraf gövdesine hizalı kalır.
Tam paragrafı taşımak gerektiğinde ParagraphFormat.setMarginLeft kullanın. Yalnızca ilk satırı taşımak istediğinizde ise ParagraphFormat.setIndent kullanın.
Aşağıdaki örnek, üç paragrafın aynı ParagraphFormat.setMarginLeft değerini 20 puan ve ParagraphFormat.setIndent değerlerini 0, 20 ve 40 puan olarak ayarlar. İlk satırlar giderek daha büyük ofsetlerle başlarken, satır başı satırları aynı sol kenar boşluğunu korur:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import FillType, Paragraph, Presentation, SaveFormat, ShapeType, TextAutofitType
from java.awt import Color
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 420, 220)
shape.getFillFormat().setFillType(FillType.NoFill)
shape.getLineFormat().getFillFormat().setFillType(FillType.Solid)
shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.GRAY)
text_frame = shape.getTextFrame()
text_frame.getTextFrameFormat().setAutofitType(TextAutofitType.Shape)
text_frame.getParagraphs().clear()
first_paragraph = Paragraph()
first_paragraph.setText("No first-line indent. Wrapped lines start at the same position as the first line.")
first_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid)
first_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
first_paragraph.getParagraphFormat().setMarginLeft(20.0)
first_paragraph.getParagraphFormat().setIndent(0.0)
second_paragraph = Paragraph()
second_paragraph.setText("First-line indent of 20 points. The first line moves to the right, while wrapped lines remain aligned to the paragraph body.")
second_paragraph.getParagraphFormat().getDefaultPortionFormat().setFillType(FillType.Solid)
second_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
second_paragraph.getParagraphFormat().setMarginLeft(20.0)
second_paragraph.getParagraphFormat().setIndent(20.0)
third_paragraph = Paragraph()
third_paragraph.setText("First-line indent of 40 points. This paragraph shows a larger first-line offset to make the effect easier to see.")
third_paragraph.getParagraphFormat().getDefaultPortionFormat().setFillType(FillType.Solid)
third_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
third_paragraph.getParagraphFormat().setMarginLeft(20.0)
third_paragraph.getParagraphFormat().setIndent(40.0)
text_frame.getParagraphs().add(first_paragraph)
text_frame.getParagraphs().add(second_paragraph)
text_frame.getParagraphs().add(third_paragraph)
presentation.save("paragraph_indent.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Sonuç:

Askı Girintisi Ayarlama
Askı girintisi, ilk satırın kalan satırların solunda başladığı bir paragraf düzenidir. Aspose.Slides içinde bu etki, ParagraphFormat.setIndent özelliğiyle oluşturulur. Indent değerini negatif yaparak ilk satırı paragraf gövdesine göre sola kaydırabilirsiniz.
Pratikte, ParagraphFormat.setMarginLeft paragraf gövdesinin sol konumunu, ParagraphFormat.setIndent ise ilk satırın bu kenar boşluğuna göre konumunu belirler. Askı girintisi oluşturmak için pozitif bir MarginLeft ve negatif bir Indent değeri ayarlayın.
Bu biçimlendirme, bibliyografiler, referanslar, sözlük girdileri ve satırların paragraf gövdesine hizalanması gereken diğer paragraflar için faydalıdır.
Aşağıdaki Python örneği, ParagraphFormat.setMarginLeft değerlerini 40 ve 60 puan, ParagraphFormat.setIndent değerlerini -20 ve -30 puan olarak eşleştirir. Her paragrafta ilk satır satır başı satırların solunda başlar; ikinci paragraf daha derin bir askı girintisine sahiptir:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import FillType, Paragraph, Presentation, SaveFormat, ShapeType, TextAutofitType
from java.awt import Color
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 420, 220)
shape.getFillFormat().setFillType(FillType.NoFill)
shape.getLineFormat().getFillFormat().setFillType(FillType.Solid)
shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.GRAY)
text_frame = shape.getTextFrame()
text_frame.getTextFrameFormat().setAutofitType(TextAutofitType.Shape)
text_frame.getParagraphs().clear()
first_paragraph = Paragraph()
first_paragraph.setText("A hanging indent is created by combining a positive left margin with a negative indent. The first line starts to the left, while wrapped lines align with the paragraph body.")
first_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid)
first_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
first_paragraph.getParagraphFormat().setMarginLeft(40.0)
first_paragraph.getParagraphFormat().setIndent(-20.0)
second_paragraph = Paragraph()
second_paragraph.setText("This second example uses a deeper hanging indent so the difference between the first line and the wrapped lines is easier to compare.")
second_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid)
second_paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
second_paragraph.getParagraphFormat().setMarginLeft(60.0)
second_paragraph.getParagraphFormat().setIndent(-30.0)
text_frame.getParagraphs().add(first_paragraph)
text_frame.getParagraphs().add(second_paragraph)
presentation.save("hanging_indent.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Sonuç:

Paragraf Sonu Koşul Özelliklerini Ayarlama
Paragraph.setEndParagraphPortionFormat yöntemi, paragraf son işaretinin biçimlendirmesini kontrol eder. Aşağıdaki örnek, PortionFormat oluşturur; BasePortionFormat.setFontHeight 48 puan, BasePortionFormat.setLatinFont ise Times New Roman olarak ayarlanır. Bu format ikinci paragrafın son işaretine atanır, görünür metin bölümleri değiştirilmez:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import FontData, Paragraph, Portion, PortionFormat, Presentation, SaveFormat, ShapeType
presentation = Presentation("Test.pptx")
try:
slide = presentation.getSlides().get_Item(0)
shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 10, 10, 200, 250)
text_frame = shape.getTextFrame()
text_frame.getParagraphs().clear()
first_paragraph = Paragraph()
first_portion = Portion("Sample text")
first_paragraph.getPortions().add(first_portion)
second_paragraph = Paragraph()
second_portion = Portion("Sample text 2")
second_paragraph.getPortions().add(second_portion)
end_paragraph_format = PortionFormat()
end_paragraph_format.setFontHeight(48)
latin_font = FontData("Times New Roman")
end_paragraph_format.setLatinFont(latin_font)
second_paragraph.setEndParagraphPortionFormat(end_paragraph_format)
text_frame.getParagraphs().add(first_paragraph)
text_frame.getParagraphs().add(second_paragraph)
presentation.save("end_paragraph_format.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Render Edilen Satırları Sayma
Satır sonlarındaki otomatik kaydırma ve noktalama işaretlerini etkileyen paragraf kuralları için bkz. Satır Kırmayı Kontrol Et ve Askı Noktalama İşaretlerini Kontrol Et.
Paragraph.getLinesCount kullanarak bir paragrafın metin yerleşiminden sonra kapladığı satırları sayabilirsiniz; otomatik kaydırma da dahildir. Bu, sunum şablonlarında metin uzunluğunu ve yerleşimini kontrol ederken faydalıdır.
Bir paragraf, TextFrame.getParagraphs içinde bir öğedir ve birden çok render edilen satır kaplayabilir. Paragraf içinde açık bir satır sonu, yeni bir paragraf oluşturmadan yeni bir satır oluşturur. Otomatik kaydırma, mevcut genişliğe göre satırları oluşturur ve metne açık satır sonu karakteri eklemez. Bu yüzden paragrafları veya satır sonu karakterlerini saymak, render edilen satır sayısını vermez.
Aşağıdaki örnek bir metin şekli oluşturur, satırlarını sayar, şeklin genişliğini daraltır ve ardından metni daha kısa bir dizeyle değiştirir. Örnek, TextFrameFormat.setWrapText ile kaydırmayı etkinleştirir ve TextFrameFormat.setAutofitType ile otomatik sığdırmayı devre dışı bırakır; böylece şekil genişliği kaydırmayı kontrol eder, metni otomatik olarak küçültmez veya şekli yeniden boyutlandırmaz. Her ölçüm Paragraph.getLinesCount kullanır. Şekil boyutları puan cinsindendir. Son olarak, örnek başka bir paragraf ekler ve metin çerçevesi boyunca satır sayımlarını toplar.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import NullableBool, Paragraph, Presentation, ShapeType, TextAutofitType
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 400, 200)
text_frame = shape.getTextFrame()
text_frame.getTextFrameFormat().setWrapText(NullableBool.True_)
text_frame.getTextFrameFormat().setAutofitType(TextAutofitType.None_)
paragraph = text_frame.getParagraphs().get_Item(0)
paragraph.getParagraphFormat().getDefaultPortionFormat().setFontHeight(20)
paragraph.setText("This text demonstrates how automatic wrapping changes the number of rendered lines.")
print("Original width:", paragraph.getLinesCount())
shape.setWidth(150)
print("Narrower shape:", paragraph.getLinesCount())
paragraph.setText("Short text.")
print("Shorter text:", paragraph.getLinesCount())
second_paragraph = Paragraph()
second_paragraph.setText("Another paragraph.")
second_paragraph.getParagraphFormat().getDefaultPortionFormat().setFontHeight(20)
text_frame.getParagraphs().add(second_paragraph)
total_line_count = 0
for current_paragraph in text_frame.getParagraphs():
total_line_count += current_paragraph.getLinesCount()
print("Total lines in the text frame:", total_line_count)
finally:
presentation.dispose()
Bu metin ve bu boyutlarla, şekli daraltmak satır sayısını artırırken, metni kısa dizeyle değiştirmek azaltır. Kesin sayılar, kullanılan yazı tipi ve ikamesi, yazı tipi boyutu, kenar boşlukları, girinti, kaydırma ve otomatik sığdırma ayarlarına göre değişebilir. Bir şablonu kontrol ederken hedef ortam için planlanan yazı tiplerini ve yerleşim ayarlarını kullanın.
Satır sayısı tek başına metnin kapsayıcısının dışına taşınıp taşmadığını belirlemez. Kullanılabilir yükseklik, satır yüksekliği, paragraf ve satır aralığı ve otomatik sığdırma davranışı da önemlidir; kaydırma devre dışı bırakıldığında tek bir satır bile kullanılabilir genişliği aşabilir.
Paragraf İçeriğini İçe/Dışa Aktarma
HTML kullanarak metni bir sunuma getirebilir veya mevcut paragrafları dışa aktararak sunum dışı kullanabilirsiniz. Bu bölüm ayrıca paragrafı resim olarak render etme ve tablo hücrelerindeki paragrafları gösterme konularını da kapsar.
Paragraflara HTML Metni İçe Aktarma
ParagraphCollection.addFromHtml kullanarak HTML işaretlemesini metin çerçevesindeki paralarlara ve bölümlere dönüştürebilirsiniz.
Bu Python örneği bir dosyadan HTML okur ve işaretlemeyi ParagraphCollection.addFromHtml yöntemine iletir. Metin çerçevesinin varsayılan paragrafını temizler, böylece çerçeve yalnızca içe alınan içeriği içerir. Dikdörtgen şekil slaytın her iki tarafında 10 puan kenar boşluğu bırakır:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import FillType, Presentation, SaveFormat, ShapeType
from pathlib import Path
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
shape_width = presentation.getSlideSize().getSize().getWidth() - 20
shape_height = presentation.getSlideSize().getSize().getHeight() - 20
shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 10, 10, shape_width, shape_height)
shape.getFillFormat().setFillType(FillType.NoFill)
shape.getTextFrame().getParagraphs().clear()
try:
html = Path("file.html").read_text(encoding="utf-8")
shape.getTextFrame().getParagraphs().addFromHtml(html)
presentation.save("html_text.pptx", SaveFormat.Pptx)
except OSError as exception:
print("The HTML file could not be read: " + str(exception))
finally:
presentation.dispose()
Paragraf Metnini HTML’ye Dışa Aktarma
ParagraphCollection.exportToHtml kullanarak seçili paragraf aralığını HTML olarak dışa aktarabilirsiniz. Sunum metnini bir web sitesinde yeniden kullanırken, sunumun yazı tipi, boyutu veya metin kutusu stillerini taşımadan yerel vurgulamayı korumak isteyebilirsiniz. TextToHtmlConversionOptions.setTextInheritanceLimit ayarı, dışa aktarımın biçimlendirme kalıtımını ne kadar takip edeceğini kontrol eder.
örnek sunumu indirin ve uygulamanın çalışma dizinine koyun. İlk slaytı, “Inherited text; explicit red 18pt” metnini içeren bir metin şekli içerir. Giriş, kasıtlı olarak birkaç biçimlendirme seviyesi kullanır:
- Sunumun varsayılan metin stili Calibri ve 28 pt boyutunu sağlar.
- Metin çerçevesinin birinci seviye metin stili mavi metin ve italik sağlar.
- Paragraf açıkça kalın metin ayarlar.
- İkinci bölüm, “explicit red 18pt”, açıkça kırmızı metin ve 18 pt boyut ayarlar.
Aşağıdaki tam örnek aynı paragrafları üç kez dışa aktarır. TextToHtmlConversionOptions dışa aktarım yöntemine geçirilir ve her sonuç ayrı bir UTF-8 HTML dosyası olarak yazılır:
import jpype
import asposeslides
from pathlib import Path
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import AutoShape, Presentation, TextInheritanceLimit, TextToHtmlConversionOptions
presentation = Presentation("text-inheritance.pptx")
try:
shape = presentation.getSlides().get_Item(0).getShapes().get_Item(0)
if isinstance(shape, AutoShape):
text_frame = shape.getTextFrame()
if text_frame is not None:
paragraphs = text_frame.getParagraphs()
modes = [TextInheritanceLimit.All, TextInheritanceLimit.TextBox, TextInheritanceLimit.ParagraphOnly]
file_names = ["All.html", "TextBox.html", "ParagraphOnly.html"]
for mode, file_name in zip(modes, file_names):
options = TextToHtmlConversionOptions()
options.setTextInheritanceLimit(mode)
html = paragraphs.exportToHtml(0, paragraphs.getCount(), options)
try:
Path(file_name).write_text(str(html), encoding="utf-8")
except OSError as exception:
print("The HTML file could not be written: " + str(exception))
else:
print("The first shape does not contain a text frame.")
else:
print("The first shape is not a text shape.")
finally:
presentation.dispose()
Oluşturulan HTML, ilk bölüm için aşağıdaki CSS’i içerir, “Inherited text; “. Tablo ilgili bildirimleri listeler; diğer oluşturulan özellikler açıklık için atlanmıştır.
| Çıktı | İlk bölüm için ilgili CSS | Görünüm |
|---|---|---|
All.html |
font-family:'Calibri'; font-size:28pt; color:#0000ff; font-style:italic; font-weight:bold |
Mavi, kalın, italik metin, Calibri’de 28 pt, bu yazı tipi mevcutsa. |
TextBox.html |
color:#0000ff; font-style:italic; font-weight:bold |
Mavi, kalın, italik metin; çevre sayfa veya tarayıcı yazı tipi ailesi ve boyutunu sağlar. |
ParagraphOnly.html |
font-weight:bold |
Kalın metin; çevre sayfa veya tarayıcı yazı tipi ailesi, boyutu, rengi ve italik stili sağlar. |
İkinci bölüm color:#ff0000, font-size:18pt ve font-weight:bold değerlerini tüm üç çıktıda korur. Ayrıca All ve TextBox içinde italik, ParagraphOnly içinde ise italik değildir. Kalıtımı kısıtlamak, bir paragraf veya bölüme açıkça atanmış biçimlendirmeyi kaldırmaz.
Dosyaları aynı tarayıcıda açarak karşılaştırın. Kısıtlı iki modda atlanan CSS özellikleri hedef sayfanın stilleri veya tarayıcı varsayılanları tarafından çözülür; bu nedenle ortaya çıkan görünüm HTML’nin nerede kullanıldığına bağlıdır.
HTML Panoya Fragment Oluşturma
TextToHtmlConversionOptions.setAddClipboardFragmentHeader kullanarak ParagraphCollection.exportToHtml çıktısının formatını seçebilirsiniz. False ise yöntem normal HTML döndürür. True ise Windows panosu başlığı ekler. StartHTML ve EndHTML alanları HTML’nin UTF-8 bayt ofsetlerini, StartFragment ve EndFragment alanları ise yapıştırılacak içeriği sınırlar.
Bu örnek iki biçimlendirilmiş paragrafı normal HTML (html) ve bir HTML pano fragmenti (clipboard_html) olarak dışa aktarır. Bir uygulama daha sonra fragmenti sistem panosuna yazabilir. Pano API’si ham bayt bekliyorsa, clipboard_html.encode("utf-8") kullanarak fragmenti BOM olmadan kodlayın.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, ShapeType, TextToHtmlConversionOptions
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 0, 0, 400, 100)
paragraphs = shape.getTextFrame().getParagraphs()
paragraphs.clear()
paragraphs.addFromHtml("<p>Status: <b>Ready</b></p><p><i>Next steps</i></p>")
options = TextToHtmlConversionOptions()
options.setAddClipboardFragmentHeader(False)
html = str(paragraphs.exportToHtml(0, paragraphs.getCount(), options))
options.setAddClipboardFragmentHeader(True)
clipboard_html = str(paragraphs.exportToHtml(0, paragraphs.getCount(), options))
finally:
presentation.dispose()
Paragrafı Görüntü Olarak Render Etme
Paragraph.getImage bireysel paragrafı doğrudan render eder ve bir görüntü nesnesi döndürür. Sonucu save yöntemiyle dosyaya veya akıma kaydedebilirsiniz. İçeren şekli render etmenize veya bitmap’i manuel olarak kırpmanıza gerek yoktur.
Paragraph.getImage paragraf bulunamazsa, geçerli bir render sınırı yoksa veya render edilemezse None dönebilir. Sonucu kaydetmeden önce kontrol edin ve görüntüyü kullandıktan sonra serbest bırakın.
Varsayılan Ölçekte Paragrafı Render Etme
Örnek, ilk şekli üç paragraf içeren bir metin kutusu olan tek slaytlı bir sunum kullanır.

Aşağıdaki örnek, indeks 1’deki ikinci paragrafı seçer ve Paragraph.getImage metodunu ölçek parametresi olmadan çağırır. Şeklin gerekli paragrafı içerdiğini ve render işleminin bir görüntü döndürdüğünü kontrol eder, ardından save yöntemiyle PNG formatında kaydeder. finally bloğu görüntüyü kullanım sonrası serbest bırakır.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import AutoShape, ImageFormat, Presentation
presentation = Presentation("sample.pptx")
try:
shape = presentation.getSlides().get_Item(0).getShapes().get_Item(0)
if isinstance(shape, AutoShape):
text_shape = shape
text_frame = text_shape.getTextFrame()
if text_frame is not None and text_frame.getParagraphs().getCount() > 1:
paragraph = text_frame.getParagraphs().get_Item(1)
paragraph_image = paragraph.getImage()
if paragraph_image is not None:
try:
paragraph_image.save("paragraph.png", ImageFormat.Png)
finally:
paragraph_image.dispose()
else:
print("The paragraph could not be rendered.")
else:
print("The expected paragraph was not found.")
else:
print("The first shape is not a text shape.")
finally:
presentation.dispose()
Sonuç:

Tablo Hücresinde Ölçekleme ile Paragrafı Render Etme
scale_x ve scale_y parametrelerini kabul eden Paragraph.getImage aşırı yüklemesini kullanarak yatay ve dikey ölçek faktörlerini ayarlayabilirsiniz. Aşağıdaki örnek bir tablo oluşturur, paragrafı ilk hücresinde varsayılan genişliğinin ve yüksekliğinin iki katı olarak render eder ve sonucu PNG olarak kaydeder.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import ImageFormat, Presentation
scale_x = 2.0
scale_y = 2.0
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
table = slide.getShapes().addTable(50, 50, [300.0], [80.0])
paragraph = table.get_Item(0, 0).getTextFrame().getParagraphs().get_Item(0)
paragraph.setText("Text in a table cell")
paragraph_image = paragraph.getImage(scale_x, scale_y)
if paragraph_image is not None:
try:
paragraph_image.save("table_paragraph.png", ImageFormat.Png)
finally:
paragraph_image.dispose()
else:
print("The paragraph could not be rendered.")
finally:
presentation.dispose()
1 ölçek faktörü ekseni varsayılan piksel boyutunda tutar. Örneğin, her iki faktör için 2 kullanmak, genişlik ve yüksekliği yaklaşık iki kat, piksel sayısını dörde kat artıran bir görüntü üretir. Daha büyük faktörler, yakınlaştırma veya yüksek çözünürlüklü çıktı için genellikle daha keskin metin üretir, ancak bellek kullanımını ve dosya boyutunu da artırır. 1 altındaki faktörler daha az detaylı, daha küçük görüntüler üretir. Paragrafın en-boy oranını korumak için eşit faktörler kullanın; farklı yatay ve dikey faktörler çıktıyı bağımsız olarak esnetir.
Shape.getImage ile bütün bir şekli render etmek, çıktının şeklin doldurmasını, kenarlığını veya diğer görsel bağlamını içermesi gerektiğinde hâlâ faydalıdır. Yalnızca paragraf resmi için Paragraph.getImage kullanın.
SSS
Metin çerçevesi içinde satır kaydırmayı tamamen devre dışı bırakabilir miyim?
Evet. TextFrameFormat.setWrapText özelliğini devre dışı bırakarak kaydırmayı kapatabilirsiniz; böylece satırlar metin çerçevesinin kenarlarında kırılmaz.
Belirli bir paragrafın slayt üzerindeki tam sınırlarını nasıl alabilirim?
Paragraph.getRect kullanarak paragrafın sınırlayıcı dikdörtgenini alabilirsiniz. Portion.getRect bireysel bir bölümün sınırlarını sağlar.
Paragraf hizalaması (sol, sağ, orta veya iki yana yaslama) nerede kontrol edilir?
ParagraphFormat.setAlignment bir paragraf düzeyi ayarıdır ve bireysel bölüm biçimlendirmesinden bağımsız olarak tüm paragrafı etkiler.
Her satır içinde farklı yazı tipi boyutlarındaki bölümleri dikey olarak hizalamak için bkz. Satır İçinde Yazı Tiplerini Hizalama.
Paragrafın bir kısmı için denetleme dilini ayarlayabilir miyim?
Evet. Bireysel bölümler için BasePortionFormat.setLanguageId ayarlayarak bir paragrafın içinde birden çok dilde metin bulunabilir.