Python üzerinden 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 şeklin içindeki 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 paragraf içinde bir metin yürütmesini temsil eder. Her bölüm kendi metnine ve karakter düzeyinde biçimlendirmeye sahip olabilir.

Bir paragraf, birden fazla bölüm kullanarak farklı fontlar, renkler, boyutlar ve diğer biçimlendirmeler içeren metinler içerebilir.

Paragraflar Oluşturma ve Biçimlendirme

Birden Çok Bölüm İçeren Paragraflar Oluşturma

Aşağıdaki adımlar, her biri üç bölüm içeren üç paragrafla bir metin çerçevesi oluşturur:

  1. Bir Presentation sınıfının örneğini oluşturun.
  2. İlgili slayta indeksini kullanarak erişin.
  3. Slayta dikdörtgen bir AutoShape ekleyin.
  4. Şeklin TextFrame öğesine erişin.
  5. Varsayılan paragrafı kullanarak metin çerçevesine iki tane daha Paragraph nesnesi ekleyin.
  6. Her paragrafın üç bölüm içermesi için yeterli sayıda Portion nesnesi ekleyin. Varsayılan paragraf zaten bir boş bölüm içerir.
  7. Her bölümün metnini ayarlayın.
  8. Portion.getPortionFormat aracılığıyla karakter düzeyinde biçimlendirme uygulayın.
  9. Değiştirilmiş sunumu kaydedin.

Bu Python örneği adımları uygular:

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

Madde İşaretli veya Numaralı Liste Oluşturma

Madde işaretleri ve numaralar, ilgili öğelerin taranmasını kolaylaştırır. Aspose.Slides’te liste ayarları BulletFormat aracılığıyla tanımlanır.

  1. Bir Presentation sınıfının örneğini oluşturun.
  2. İlgili slayta indeksini kullanarak erişin.
  3. Seçili slayta bir AutoShape ekleyin.
  4. Şeklin TextFrame öğesine erişin.
  5. Metin çerçevesindeki varsayılan paragrafı kaldırın.
  6. Sembol madde işareti için bir Paragraph oluşturun.
  7. BulletFormat.setType metodunu BulletType.Symbol olarak ayarlayın ve madde işareti karakterini belirtin.
  8. Paragraf metnini, girintiyi, madde işareti rengini ve yüksekliğini ayarlayın.
  9. Paragrafı metin çerçevesine ekleyin.
  10. İkinci bir paragraf oluşturun ve BulletFormat.setType metodunu BulletType.Numbered olarak ayarlayın.
  11. Numaralı madde işareti stilini yapılandırın ve paragrafı metin çerçevesine ekleyin.
  12. Sunumu kaydedin.

Bu Python örneği bir sembol madde işareti ve bir numaralı madde işareti oluşturur:

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()

Resim Madde İşaretleri Kullanma

Resim madde işaretleri, sembol ya da sayı yerine özelleştirilmiş bir görüntü kullanmanıza olanak tanır.

  1. Bir Presentation sınıfının örneğini oluşturun.
  2. İlgili slayta indeksini kullanarak erişin.
  3. Bir AutoShape ekleyin ve onun TextFrame öğesine erişin.
  4. Metin çerçevesindeki varsayılan paragrafı kaldırın.
  5. Madde işareti görüntüsünü yükleyin ve sunumun görüntü koleksiyonuna bir PPImage olarak ekleyin.
  6. Bir Paragraph oluşturun ve metnini ayarlayın.
  7. BulletFormat.setType metodunu BulletType.Picture olarak ayarlayın.
  8. Görüntüyü BulletFormat.getPicture ile atayın ve madde işareti yüksekliğini ayarlayın.
  9. Paragrafı metin çerçevesine ekleyin.
  10. Değiştirilmiş sunumu kaydedin.

Bu Python örneği bir resim madde işareti oluşturur:

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 Düzeyli Liste Oluşturma

ParagraphFormat.setDepth metodunu ayarlayarak paragrafları bir listenin farklı seviyelerine yerleştirebilirsiniz. En üst seviye 0 derinliğe sahiptir.

  1. Bir Presentation oluşturun ve bir slayta erişin.
  2. Bir AutoShape ekleyin ve metin çerçevesindeki varsayılan paragrafı temizleyin.
  3. Dört paragraf oluşturun ve madde işareti sembollerini yapılandırın.
  4. Onların ParagraphFormat.setDepth değerlerini 0, 1, 2 ve 3 olarak ayarlayın.
  5. Paragrafları metin çerçevesine ekleyin ve sunumu kaydedin.

Bu Python örneği dört seviyeli bir madde işaretli liste oluşturur:

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

Bir numaralı paragraf için görüntülenecek başlangıç numarasını ayarlamak üzere BulletFormat.setNumberedBulletStartWith kullanın.

  1. Bir Presentation oluşturun ve bir slayta bir AutoShape ekleyin.
  2. Şeklin metin çerçevesindeki varsayılan paragrafı temizleyin.
  3. Üç numaralı paragraf oluşturun.
  4. İlgili paragraflar için BulletFormat.setNumberedBulletStartWith metodunu sırasıyla 2, 3 ve 7 olarak ayarlayın.
  5. Paragrafları metin çerçevesine ekleyin ve sunumu kaydedin.

Bu Python örneği her paragraf için özel bir başlangıç numarası atar:

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

İlk Satır Girintisi Ayarlama

ParagraphFormat.setIndent metodunu kullanarak bir paragrafın ilk satır girintisini kontrol edin. Bu yöntem yalnızca ilk satırı paragrafın sol kenar boşluğuna göre kaydırır. Pozitif bir değer ilk satırı sağa kaydırırken, geri kalan satırlar paragraf gövdesine göre hizalanır.

Tüm paragrafı hareket ettirmeniz gerektiğinde ParagraphFormat.setMarginLeft metodunu kullanın. Yalnızca ilk satırı hareket ettirmeniz gerektiğinde ise ParagraphFormat.setIndent kullanın.

Aşağıdaki örnek, birkaç paragraf oluşturur ve ilk satır girintisinin paragraf düzenine nasıl etki ettiğini göstermek için farklı ParagraphFormat.setIndent değerleri uygular.

  1. Bir Presentation sınıfının bir örneğini oluşturun.
  2. Hedef slayta erişin.
  3. Slayta dikdörtgen bir AutoShape ekleyin.
  4. Şeklin TextFrame öğesine erişin ve varsayılan paragrafı kaldırın.
  5. Birkaç paragraf oluşturun ve bunlar için farklı ParagraphFormat.setIndent değerleri ayarlayın.
  6. Paragrafları metin çerçevesine ekleyin.
  7. Değiştirilmiş sunumu kaydedin.

Bu kod bir paragraf girintisinin nasıl ayarlanacağını gösterir:

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ç:

Paragrafların ilk satır girintisi

Sarkıntı Girintisi Ayarlama

Sarkıntı girintisi, ilk satırın geri kalan satırların solunda başladığı bir paragraf düzenidir. Aspose.Slides’te bu etkiyi ParagraphFormat.setIndent ile oluşturursunuz. İlk satırı paragraf gövdesine göre sola kaydırmak için negatif bir değer verin.

Uygulamada, ParagraphFormat.setMarginLeft paragraf gövdesinin sol konumunu tanımlar ve ParagraphFormat.setIndent ilk satırın bu kenar boşluğuna göre konumunu tanımlar. Sarkıntı girintisi oluşturmak için ParagraphFormat.setMarginLeft metoduna pozitif bir değer, ParagraphFormat.setIndent metoduna ise negatif bir değer verin.

Bu biçimlendirme, satırların paragraf gövdesi altında, ilk satırın ilk karakteri altında değil, hizalanması gereken bibliyografyalar, referanslar, sözlük girdileri ve diğer paragraflar için yararlıdır.

  1. Bir Presentation sınıfının örneğini oluşturun.
  2. Hedef slayta erişin.
  3. Slayta dikdörtgen bir AutoShape ekleyin.
  4. Şeklin TextFrame öğesine erişin ve varsayılan paragrafı kaldırın.
  5. Paragraflar oluşturun ve her paragraf için ParagraphFormat.setMarginLeft metoduna pozitif bir değer geçirin.
  6. Sarkıntı girintisi etkisini yaratmak için ParagraphFormat.setIndent metoduna negatif bir değer geçirin.
  7. Paragrafları metin çerçevesine ekleyin.
  8. Değiştirilmiş sunumu kaydedin.

Bu kod bir paragraf için sarkıntı girintisinin nasıl ayarlanacağını gösterir:

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ç:

Paragrafların sarkıntı girintisi

Paragraf Sonu Çalıştırma Özelliklerini Ayarlama

Paragraph.setEndParagraphPortionFormat paragraf son işaretinin biçimlendirmesini kontrol eder. Aşağıdaki örnek, ikinci paragrafın son işaretine bir yazı tipi boyutu ve Latin yazı tipi atar:

  1. Bir Presentation yükleyin ve bir slayta erişin.
  2. Bir AutoShape ekleyin ve varsayılan paragrafını temizleyin.
  3. İki paragraf oluşturun ve bunlara metin bölümleri ekleyin.
  4. İkinci paragrafın son işareti için bir PortionFormat oluşturun.
  5. BasePortionFormat.setFontHeight ve BasePortionFormat.setLatinFont yöntemlerini ayarlayın.
  6. Biçimi Paragraph.setEndParagraphPortionFormat ile atayın ve sunumu kaydedin.
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()

Paragraf İçeriği İçeri Aktarma ve Dışarı Aktarma

HTML Metnini Paragraflara İçe Aktarma

ParagraphCollection.addFromHtml metodunu kullanarak HTML işaretlemesini bir metin çerçevesindeki paragraflara ve bölümlere dönüştürün.

  1. Bir Presentation sınıfının örneğini oluşturun.
  2. Bir slayta erişin ve bir AutoShape ekleyin.
  3. Şeklin TextFrame öğesine erişin ve varsayılan paragrafı temizleyin.
  4. Kaynak HTML dosyasını okuyun.
  5. HTML dizesini ParagraphCollection.addFromHtml metoduna geçirin.
  6. Değiştirilmiş sunumu kaydedin.

Bu Python örneği HTML’yi bir metin çerçevesine içe aktarı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

Seçilen paragraf aralığını HTML olarak dışa aktarmak için ParagraphCollection.exportToHtml metodunu kullanın.

  1. Bir Presentation sınıfının örneğini oluşturun ve istenen sunumu yükleyin.
  2. Slayta erişin ve metni içeren AutoShape öğesini bulun.
  3. Şeklin TextFrame öğesine erişin.
  4. ParagraphCollection.exportToHtml metodunu başlangıç paragraf indeksi ve dışa aktarılacak paragraf sayısı ile çağırın.
  5. Dönene HTML dizesini bir dosyaya yazın.

Bu Python örneği ilk metin şeklinin tüm paragraflarını dışa aktarır:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import AutoShape, Presentation
from pathlib import Path

presentation = Presentation("ExportingHTMLText.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:
            paragraphs = text_frame.getParagraphs()
            html = paragraphs.exportToHtml(0, paragraphs.getCount(), None)
            try:
                Path("paragraphs.html").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()

Paragrafı Görüntü Olarak Oluşturma

Paragraph.getImage tek bir paragrafı doğrudan render eder ve bir görüntü nesnesi döndürür. Sonucu save yöntemiyle bir dosyaya ya da akışa kaydedin. İçeren şekli render etmenize ya da bitmap’i elle kırpmanıza gerek yoktur.

Paragraph.getImage paragraf ebeveyn koleksiyonunda bulunamazsa, geçerli bir renderleme sınırı yoksa ya da renderlenemezse None dönebilir. Kaydetmeden önce sonucu kontrol edin ve kullanımdan sonra dönen görüntüyü serbest bırakın.

Paragrafı Varsayılan Ölçekte Render Etme

sample.pptx adlı bir sunum dosyamızın bir slaytı olduğunu ve ilk şeklinin üç paragraf içeren bir metin kutusu olduğunu varsayalım.

Üç paragraf içeren metin kutusu

Aşağıdaki örnek, ikinci paragrafı normal bir metin şeklinin içinde varsayılan ölçekte render eder ve dönen görüntüyü PNG formatında kaydeder. finally bloğu, görüntünün doğru şekilde serbest bırakılmasını sağlar.

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ç:

Paragraf görüntüsü

Bir Tablo Hücresinde Ölçeklendirme 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 ayarlayın. Aşağıdaki örnek bir tablo oluşturur, paragrafı ilk hücresinde varsayılan genişliğinin ve yüksekliğinin iki katına çıkararak render eder ve sonucu PNG görüntüsü 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ü, ilgili ekseni varsayılan piksel boyutunda tutar. Örneğin, her iki faktör için 2 kullanmak, genişliği ve yüksekliği yaklaşık iki kat olan bir görüntü üretir ve bu da dört kat piksel anlamına gelir. Daha büyük faktörler genellikle yakınlaştırma veya yüksek çözünürlüklü çıktı için daha keskin metin üretir, ancak bellek kullanımını ve dosya boyutunu artırır. 1‘in 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 uzatır.

Çıktının şeklin dolgu, kenarlık veya diğer görsel bağlamını içermesi gerektiğinde Shape.getImage ile tüm şekli render etmek yararlıdır. Yalnızca paragraf görüntüsü için Paragraph.getImage kullanın.

SSS

Metin çerçevesi içinde satır kaydırmayı tamamen devre dışı bırakabilir miyim?
Evet. Satırların metin çerçevesinin kenarlarında kırılmasını önlemek için TextFrameFormat.setWrapText metodunu ayarlayarak kaydırmayı devre dışı bırakın.

Belirli bir paragrafın slayt üzerindeki tam sınırlarını nasıl alabilirim?
Paragraph.getRect metodunu kullanarak paragrafın sınırlayıcı dikdörtgenini alın. Portion.getRect bireysel bir bölümün sınırlarını sağlar.

Paragraf hizalaması (sol, sağ, ortalanmış veya iki yana yaslanmış) nerede kontrol edilir?
ParagraphFormat.setAlignment paragraf düzeyinde bir ayardır ve bireysel bölüm biçimlendirmesinden bağımsız olarak tüm paragrafı etkiler.

Paragrafın bir kısmı için düzeltme dilini ayarlayabilir miyim?
Evet. Bireysel bölümler için BasePortionFormat.setLanguageId metodunu ayarlayın; böylece bir paragraf birden fazla dilde metin içerebilir.