Kelola Paragraf Teks PowerPoint di Python via Java
Gambaran Umum
Aspose.Slides for Python via Java merepresentasikan teks sebagai hierarki bingkai teks, paragraf, dan bagian:
- TextFrame merepresentasikan kontainer teks dalam sebuah bentuk dan menyediakan akses ke koleksi paragrafnya.
- Paragraph merepresentasikan satu paragraf dalam bingkai teks dan menyediakan akses ke bagian‑bagian serta pemformatan level paragraf.
- Portion merepresentasikan satu run teks dalam paragraf. Setiap bagian dapat memiliki teks dan pemformatan level karakter masing‑majinya.
Dengan demikian, sebuah paragraf dapat berisi teks dengan font, warna, ukuran, dan pemformatan lain yang berbeda dengan menggunakan beberapa bagian.
Membuat dan Memformat Paragraf
Membuat Paragraf dengan Beberapa Bagian
Langkah‑langkah berikut membuat bingkai teks dengan tiga paragraf, masing‑masing berisi tiga bagian:
- Buat sebuah instance kelas Presentation.
- Akses slide yang relevan melalui indeksnya.
- Tambahkan sebuah AutoShape persegi panjang ke slide.
- Akses TextFrame dari bentuk.
- Gunakan paragraf default dan tambahkan dua objek Paragraph lagi ke bingkai teks.
- Tambahkan cukup objek Portion sehingga tiap paragraf berisi tiga bagian. Paragraf default sudah berisi satu bagian kosong.
- Atur teks setiap bagian.
- Terapkan pemformatan level karakter melalui Portion.getPortionFormat.
- Simpan presentasi yang telah dimodifikasi.
Contoh Python berikut mengimplementasikan langkah‑langkah tersebut:
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()
Membuat Daftar Berbulu dan Bernomor
Membuat Daftar Berbulu atau Bernomor
Bulu dan penomoran memudahkan pemindaian item terkait. Di Aspose.Slides, pengaturan daftar didefinisikan melalui BulletFormat.
- Buat sebuah instance kelas Presentation.
- Akses slide yang relevan melalui indeksnya.
- Tambahkan sebuah AutoShape ke slide yang dipilih.
- Akses TextFrame dari bentuk.
- Hapus paragraf default dari bingkai teks.
- Buat sebuah Paragraph untuk bulu simbol.
- Atur BulletFormat.setType ke BulletType.Symbol dan tentukan karakter bulu.
- Atur teks paragraf, indent, warna bulu, dan tinggi bulu.
- Tambahkan paragraf ke bingkai teks.
- Buat paragraf kedua dan atur BulletFormat.setType ke BulletType.Numbered.
- Konfigurasikan gaya bulu bernomor dan tambahkan paragraf ke bingkai teks.
- Simpan presentasi.
Contoh Python berikut membuat bulu simbol dan bulu bernomor:
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()
Menggunakan Bulu Gambar
Bulu gambar memungkinkan Anda menggunakan gambar khusus alih‑alih simbol atau nomor.
- Buat sebuah instance kelas Presentation.
- Akses slide yang relevan melalui indeksnya.
- Tambahkan sebuah AutoShape dan akses TextFrame‑nya.
- Hapus paragraf default dari bingkai teks.
- Muat gambar bulu dan tambahkan ke koleksi gambar presentasi sebagai PPImage.
- Buat sebuah Paragraph dan atur teksnya.
- Atur BulletFormat.setType ke BulletType.Picture.
- Tetapkan gambar melalui BulletFormat.getPicture dan atur tinggi bulu.
- Tambahkan paragraf ke bingkai teks.
- Simpan presentasi yang telah dimodifikasi.
Contoh Python berikut membuat bulu gambar:
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()
Membuat Daftar Multilevel
Atur ParagraphFormat.setDepth untuk menempatkan paragraf pada level yang berbeda dalam sebuah daftar. Level teratas memiliki depth 0.
- Buat sebuah Presentation dan akses sebuah slide.
- Tambahkan sebuah AutoShape dan bersihkan paragraf default dari bingkai teksnya.
- Buat empat paragraf dan konfigurasikan simbol bulu masing‑masing.
- Atur nilai ParagraphFormat.setDepth mereka menjadi
0,1,2, dan3. - Tambahkan paragraf ke bingkai teks dan simpan presentasi.
Contoh Python berikut membuat daftar berbulu empat level:
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()
Memulai Item Daftar Bernomor dengan Nilai Kustom
Gunakan BulletFormat.setNumberedBulletStartWith untuk menentukan angka awal yang ditampilkan untuk paragraf bernomor.
- Buat sebuah Presentation dan tambahkan sebuah AutoShape ke sebuah slide.
- Bersihkan paragraf default dari bingkai teks bentuk.
- Buat tiga paragraf bernomor.
- Atur BulletFormat.setNumberedBulletStartWith menjadi
2,3, dan7untuk masing‑masing paragraf. - Tambahkan paragraf ke bingkai teks dan simpan presentasi.
Contoh Python berikut menetapkan angka mulai kustom untuk setiap paragraf:
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()
Mengontrol Tata Letak Paragraf dan Properti Akhir
Menetapkan Indent Baris Pertama
Gunakan ParagraphFormat.setIndent untuk mengontrol indent baris pertama sebuah paragraf. Metode ini hanya memindahkan baris pertama relatif terhadap margin kiri paragraf. Nilai positif menggeser baris pertama ke kanan, sementara baris‑baris lainnya tetap sejajar dengan badan paragraf.
Gunakan ParagraphFormat.setMarginLeft bila Anda perlu memindahkan seluruh paragraf. Gunakan ParagraphFormat.setIndent bila Anda hanya perlu memindahkan baris pertama.
Contoh di bawah membuat beberapa paragraf dan menerapkan nilai ParagraphFormat.setIndent yang berbeda untuk memperlihatkan bagaimana indent baris pertama memengaruhi tata letak paragraf.
- Buat sebuah instance kelas Presentation.
- Akses slide target.
- Tambahkan sebuah AutoShape persegi panjang ke slide.
- Akses TextFrame bentuk dan hapus paragraf default.
- Buat beberapa paragraf dan atur nilai ParagraphFormat.setIndent yang berbeda untuk masing‑masing.
- Tambahkan paragraf ke bingkai teks.
- Simpan presentasi yang telah dimodifikasi.
Kode berikut menunjukkan cara menetapkan indent paragraf:
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().getFillFormat().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().getFillFormat().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()
Hasilnya:

Menetapkan Indent Gantung
Indent gantung adalah tata letak paragraf di mana baris pertama dimulai lebih ke kiri dibandingkan baris‑baris berikutnya. Di Aspose.Slides, Anda membuat efek ini dengan ParagraphFormat.setIndent. Berikan nilai negatif untuk memindahkan baris pertama ke kiri relatif terhadap badan paragraf.
Dalam praktiknya, ParagraphFormat.setMarginLeft menentukan posisi kiri badan paragraf, dan ParagraphFormat.setIndent menentukan posisi baris pertama relatif terhadap margin tersebut. Untuk membuat indent gantung, berikan nilai positif ke ParagraphFormat.setMarginLeft dan nilai negatif ke ParagraphFormat.setIndent.
Pemformatan ini berguna untuk bibliografi, referensi, entri glosarium, dan paragraf lain dimana baris yang dibungkus harus rata di bawah badan paragraf, bukan di bawah karakter pertama baris pertama.
- Buat sebuah instance kelas Presentation.
- Akses slide target.
- Tambahkan sebuah AutoShape persegi panjang ke slide.
- Akses TextFrame bentuk dan hapus paragraf default.
- Buat paragraf dan berikan nilai positif ke ParagraphFormat.setMarginLeft untuk masing‑masing paragraf.
- Berikan nilai negatif ke ParagraphFormat.setIndent untuk menciptakan efek indent gantung.
- Tambahkan paragraf ke bingkai teks.
- Simpan presentasi yang telah dimodifikasi.
Kode berikut menunjukkan cara menetapkan indent gantung untuk sebuah paragraf:
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()
Hasilnya:

Menetapkan Properti Akhir Paragraf
Paragraph.setEndParagraphPortionFormat mengontrol pemformatan tanda akhir paragraf. Contoh berikut menetapkan ukuran font dan font Latin ke tanda akhir paragraf kedua:
- Muat sebuah Presentation dan akses sebuah slide.
- Tambahkan sebuah AutoShape dan bersihkan paragraf defaultnya.
- Buat dua paragraf dan tambahkan bagian‑bagian teks ke dalamnya.
- Buat sebuah PortionFormat untuk tanda akhir paragraf kedua.
- Atur BasePortionFormat.setFontHeight dan BasePortionFormat.setLatinFont.
- Tetapkan format dengan Paragraph.setEndParagraphPortionFormat dan simpan presentasi.
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()
Mengimpor dan Mengekspor Konten Paragraf
Mengimpor Teks HTML ke Paragraf
Gunakan ParagraphCollection.addFromHtml untuk mengonversi markup HTML menjadi paragraf dan bagian dalam sebuah bingkai teks.
- Buat sebuah instance kelas Presentation.
- Akses sebuah slide dan tambahkan sebuah AutoShape.
- Akses TextFrame bentuk dan bersihkan paragraf defaultnya.
- Baca berkas HTML sumber.
- Berikan string HTML ke ParagraphCollection.addFromHtml.
- Simpan presentasi yang telah dimodifikasi.
Contoh Python berikut mengimpor HTML ke dalam bingkai teks:
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()
Mengekspor Teks Paragraf ke HTML
Gunakan ParagraphCollection.exportToHtml untuk mengekspor rentang paragraf terpilih sebagai HTML.
- Buat sebuah instance kelas Presentation dan muat presentasi yang diinginkan.
- Akses slide dan temukan AutoShape yang berisi teks.
- Akses TextFrame bentuk.
- Panggil ParagraphCollection.exportToHtml dengan indeks paragraf mulai dan jumlah paragraf yang akan diekspor.
- Tulis string HTML yang dikembalikan ke sebuah berkas.
Contoh Python berikut mengekspor semua paragraf dari bentuk teks pertama:
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()
Merender Paragraf sebagai Gambar
Paragraph.getImage merender sebuah paragraf individu secara langsung dan mengembalikan objek gambar. Simpan hasilnya ke berkas atau stream dengan metode save. Anda tidak perlu merender bentuk yang menampungnya atau memotong bitmap secara manual.
Paragraph.getImage dapat mengembalikan None bila paragraf tidak ditemukan dalam koleksi induknya, tidak memiliki batas render yang valid, atau tidak dapat dirender. Periksa hasilnya sebelum menyimpan dan buang gambar yang dikembalikan setelah selesai digunakan.
Merender Paragraf pada Skala Default
Misalkan kita memiliki berkas presentasi bernama sample.pptx dengan satu slide, di mana bentuk pertama adalah kotak teks yang berisi tiga paragraf.

Contoh berikut merender paragraf kedua dalam bentuk teks biasa pada skala default dan menyimpan gambar yang dikembalikan dalam format PNG. Blok finally memastikan gambar dibuang dengan benar.
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()
Hasilnya:

Merender Paragraf dalam Sel Tabel dengan Skala
Gunakan overload Paragraph.getImage yang menerima parameter scale_x dan scale_y untuk menetapkan faktor skala horizontal dan vertikal. Contoh berikut membuat sebuah tabel, merender paragraf dalam sel pertamanya dengan lebar dan tinggi dua kali skala default, dan menyimpan hasilnya sebagai gambar PNG.
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()
Faktor skala 1 mempertahankan ukuran piksel default pada sumbu tersebut. Misalnya, 2 untuk kedua faktor menghasilkan gambar yang lebar dan tingginya kira‑kira dua kali dimensi default, menghasilkan empat kali lebih banyak piksel. Faktor yang lebih besar umumnya menghasilkan teks yang lebih tajam untuk zoom atau output beresolusi tinggi, namun juga meningkatkan penggunaan memori dan ukuran berkas. Faktor di bawah 1 menghasilkan gambar lebih kecil dengan detail lebih sedikit. Gunakan faktor yang sama untuk mempertahankan rasio aspek paragraf; faktor horizontal dan vertikal yang berbeda akan meregangkan output secara terpisah.
Merender seluruh bentuk dengan Shape.getImage tetap berguna ketika output harus mencakup isian, batas, atau konteks visual bentuk. Untuk gambar hanya paragraf, gunakan Paragraph.getImage.
FAQ
Apakah saya dapat menonaktifkan pembungkus baris secara total di dalam bingkai teks?
Ya. Atur TextFrameFormat.setWrapText untuk menonaktifkan pembungkus sehingga baris tidak terputus di tepi bingkai teks.
Bagaimana cara mendapatkan batas tepat pada slide untuk paragraf tertentu?
Gunakan Paragraph.getRect untuk mengambil persegi panjang pembatas paragraf. Portion.getRect memberikan batas sebuah bagian individu.
Di mana kontrol perataan paragraf (kiri, kanan, tengah, atau justify) berada?
ParagraphFormat.setAlignment adalah pengaturan level paragraf dan berlaku untuk seluruh paragraf terlepas dari pemformatan bagian individual.
Apakah saya dapat mengatur bahasa pemeriksaan ejaan untuk bagian dari paragraf?
Ya. Atur BasePortionFormat.setLanguageId untuk bagian‑bagian individual, sehingga satu paragraf dapat berisi teks dalam berbagai bahasa.