Quản lý các đoạn văn bản PowerPoint trong Python qua Java
Tổng quan
Aspose.Slides for Python via Java biểu diễn văn bản dưới dạng một cấu trúc phân cấp gồm các khung văn bản, đoạn văn và phần:
- TextFrame đại diện cho vùng chứa văn bản trong một shape và cung cấp quyền truy cập vào bộ sưu tập đoạn văn của nó.
- Paragraph đại diện cho một đoạn văn trong một khung văn bản và cung cấp quyền truy cập vào các phần và định dạng cấp đoạn.
- Portion đại diện cho một đoạn văn bản trong một đoạn văn. Mỗi phần có thể có văn bản và định dạng cấp ký tự riêng.
Do đó, một đoạn văn có thể chứa văn bản với các phông chữ, màu sắc, kích thước và các định dạng khác nhau bằng cách sử dụng nhiều phần.
Tạo và Định dạng Đoạn văn
Tạo các đoạn văn trong khung văn bản của một shape và sử dụng các phần để định dạng các đoạn văn bản riêng lẻ. Phần này trình bày cách kết hợp nhiều phần trong mỗi đoạn văn và áp dụng các màu sắc, kích thước phông chữ và kiểu dáng khác nhau.
Tạo Đoạn Văn với Nhiều Phần
Ví dụ Python sau tạo ba đoạn văn, mỗi đoạn chứa ba đoạn văn bản trong bộ sưu tập trả về bởi Paragraph.getPortions. Nó tái sử dụng đoạn văn mặc định và phần trống của nó, sau đó thêm các đoạn và phần còn lại. Định dạng được áp dụng qua Portion.getPortionFormat: phần đầu tiên trong mỗi đoạn là màu đỏ, in đậm, và kích thước 15 điểm; phần thứ hai là màu xanh lam, in nghiêng, và 18 điểm. Phần thứ ba giữ nguyên định dạng mặc định:
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()
Tạo Danh Sách Đánh Dấu và Đánh Số
Tổ chức văn bản của đoạn văn thành các danh sách với ký hiệu, số hoặc hình ảnh tùy chỉnh. Các ví dụ trong phần này bao gồm định dạng dấu đầu dòng, danh sách đa cấp và số bắt đầu tùy chỉnh.
Tạo Danh Sách Đánh Dấu hoặc Đánh Số
Dấu đầu dòng và đánh số giúp người đọc dễ quét các mục liên quan. Trong Aspose.Slides, cài đặt danh sách được định nghĩa qua BulletFormat.
Ví dụ Python này thêm một dấu đầu dòng ký hiệu và một mục đánh số vào cùng một khung văn bản. BulletFormat.setType chọn loại dấu đầu dòng, trong khi BulletFormat.setChar cung cấp ký hiệu và BulletFormat.setNumberedBulletStyle chọn kiểu đánh số. Cả hai mục đều sử dụng dấu đầu dòng đen với chiều cao 100 % và thụt đoạn 25 điểm:
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()
Sử Dụng Dấu Đầu Dòng Hình Ảnh
Dấu đầu dòng hình ảnh cho phép bạn sử dụng một hình ảnh tùy chỉnh thay vì ký hiệu hoặc số.
Ví dụ Python này thêm một hình ảnh vào bộ sưu tập hình ảnh của bản trình bày dưới dạng một PPImage và sử dụng nó làm dấu đầu dòng cho đoạn văn. Nó gọi BulletFormat.setType để sử dụng dấu đầu dòng hình ảnh và gán hình ảnh thông qua BulletFormat.getPicture. BulletFormat.setHeight được đặt thành 100 % để kích thước dấu đầu dòng tương đối với văn bản đoạn:
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()
Tạo Danh Sách Đa Cấp
Đặt ParagraphFormat.setDepth để đặt các đoạn văn ở các mức độ khác nhau của một danh sách. Mức cao nhất có độ sâu 0.
Ví dụ Python này gán các giá trị ParagraphFormat.setDepth từ 0 đến 3 cho bốn đoạn văn, tạo một cây phân cấp trong một khung văn bản. Mỗi đoạn văn sử dụng dấu đầu dòng ký hiệu, với BulletFormat.setChar luân phiên giữa dấu tròn và dấu gạch nối để phân biệt các mức lân cận:
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()
Bắt Đầu Các Mục Đánh Số Với Giá Trị Tùy Chỉnh
Sử dụng BulletFormat.setNumberedBulletStartWith để đặt số ban đầu hiển thị cho một đoạn văn được đánh số.
Ví dụ Python này đặt BulletFormat.setType để sử dụng đánh số cho ba đoạn văn và gán các giá trị BulletFormat.setNumberedBulletStartWith là 2, 3 và 7. Giá trị bắt đầu được cấu hình riêng cho mỗi đoạn, cho phép các số hiển thị bỏ qua các giá trị:
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()
Kiểm Soát Bố Cục Đoạn Văn Và Thuộc Tính Kết Thúc
Điều chỉnh lề và thụt đoạn để kiểm soát vị trí của dòng đầu và các dòng gói. Bạn cũng có thể định dạng dấu kết thúc đoạn riêng biệt so với các phần văn bản hiển thị.
Đặt Thụt Dòng Dòng Đầu
Sử dụng phương thức ParagraphFormat.setIndent để kiểm soát thụt dòng đầu của một đoạn. Cài đặt này chỉ di chuyển dòng đầu so với lề trái của đoạn. Giá trị dương dịch dòng đầu sang phải, trong khi các dòng còn lại vẫn căn chỉnh với thân đoạn.
Sử dụng ParagraphFormat.setMarginLeft khi bạn cần di chuyển toàn bộ đoạn. Sử dụng ParagraphFormat.setIndent khi bạn chỉ muốn di chuyển dòng đầu.
Ví dụ sau đưa ba đoạn văn cùng một giá trị ParagraphFormat.setMarginLeft là 20 điểm và các giá trị ParagraphFormat.setIndent là 0, 20 và 40 điểm. Các dòng đầu bắt đầu tại các vị trí lệch dần tăng, trong khi các dòng gói giữ cùng lề trái:
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()
Kết quả:

Đặt Thụt Dòng Treo
Thụt dòng treo là bố cục đoạn trong đó dòng đầu bắt đầu ở bên trái các dòng còn lại. Trong Aspose.Slides, bạn tạo hiệu ứng này bằng thuộc tính ParagraphFormat.setIndent. Đặt Indent thành giá trị âm để dịch dòng đầu sang trái so với thân đoạn.
Trong thực tế, ParagraphFormat.setMarginLeft xác định vị trí trái của thân đoạn, và ParagraphFormat.setIndent xác định vị trí của dòng đầu so với lề đó. Để tạo thụt dòng treo, đặt giá trị MarginLeft dương và giá trị Indent âm.
Định dạng này hữu ích cho thư mục, tham khảo, mục giải thuật và các đoạn khác nơi các dòng gói phải căn dưới thân đoạn thay vì dưới ký tự đầu tiên của dòng đầu.
Ví dụ Python sau ghép các giá trị ParagraphFormat.setMarginLeft là 40 và 60 điểm với các giá trị ParagraphFormat.setIndent là -20 và -30 điểm. Trong mỗi đoạn, dòng đầu bắt đầu bên trái các dòng gói; đoạn thứ hai có thụt dòng treo sâu hơn:
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()
Kết quả:

Đặt Thuộc Tính Chạy Đoạn Kết Thúc
Phương thức Paragraph.setEndParagraphPortionFormat kiểm soát định dạng của dấu kết thúc đoạn. Ví dụ sau tạo một PortionFormat với BasePortionFormat.setFontHeight đặt thành 48 điểm và BasePortionFormat.setLatinFont đặt thành Times New Roman. Nó gán định dạng này cho dấu kết thúc của đoạn thứ hai, để lại các phần văn bản hiển thị không thay đổi:
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()
Đếm Số Dòng Được Kết Xuất
Đối với các quy tắc đoạn ảnh hưởng tới việc ngắt dòng tự động và dấu câu ở cuối dòng, xem Control Line Breaking và Control Hanging Punctuation.
Sử dụng Paragraph.getLinesCount để đếm số dòng mà một đoạn chiếm sau khi bố trí văn bản, bao gồm việc ngắt dòng tự động. Điều này hữu ích khi kiểm tra độ dài và bố cục văn bản trong mẫu bản trình bày.
Một đoạn là một mục trong TextFrame.getParagraphs, và nó có thể chiếm nhiều dòng kết xuất. Một dấu ngắt dòng rõ ràng bên trong đoạn buộc tạo một dòng mới mà không tạo đoạn mới. Việc ngắt dòng tự động tạo các dòng dựa trên chiều rộng có sẵn mà không chèn dấu ngắt dòng vào văn bản. Do đó, đếm số đoạn hoặc ký tự ngắt dòng không cho kết quả số dòng được kết xuất.
Ví dụ sau tạo một hình dạng văn bản, đếm các dòng của nó, thu hẹp hình dạng, sau đó thay thế văn bản bằng một chuỗi ngắn hơn. Ví dụ bật tính năng gói bằng TextFrameFormat.setWrapText và tắt tự động vừa bằng TextFrameFormat.setAutofitType để chiều rộng hình dạng kiểm soát việc gói mà không tự động thu nhỏ văn bản hay thay đổi kích thước hình dạng. Mỗi phép đo sử dụng Paragraph.getLinesCount. Kích thước hình dạng tính bằng điểm. Cuối cùng, ví dụ thêm một đoạn nữa và tổng hợp số dòng qua khung văn bản.
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()
Với văn bản và các kích thước này, thu hẹp hình dạng làm tăng số dòng, trong khi thay thế bằng chuỗi ngắn làm giảm số dòng. Các số đếm chính xác có thể thay đổi tùy thuộc vào khả năng có sẵn của phông chữ và sự thay thế, kích thước phông, lề, thụt, gói và cài đặt tự vừa. Hãy sử dụng các phông và cài đặt bố cục dự kiến cho môi trường đích khi kiểm tra mẫu.
Số dòng một mình không quyết định liệu văn bản có tràn ra khỏi vùng chứa hay không. Chiều cao khả dụng, độ cao dòng, khoảng cách đoạn và dòng, và hành vi tự vừa cũng ảnh hưởng; ngay cả một dòng duy nhất cũng có thể vượt quá chiều rộng khi tắt gói.
Nhập và Xuất Nội Dung Đoạn Văn
Sử dụng HTML để đưa văn bản vào bản trình bày hoặc xuất các đoạn hiện có để sử dụng bên ngoài. Phần này cũng chỉ ra cách kết xuất các đoạn riêng lẻ thành hình ảnh, bao gồm các đoạn trong ô bảng.
Nhập Văn Bản HTML Vào Đoạn Văn
Sử dụng ParagraphCollection.addFromHtml để chuyển đổi markup HTML thành các đoạn và phần trong một khung văn bản.
Ví dụ Python này đọc HTML từ tệp và truyền markup cho ParagraphCollection.addFromHtml. Nó xóa đoạn mặc định của khung văn bản để khung chỉ chứa nội dung đã nhập. Hình chữ nhật để lại lề 10 điểm ở mỗi cạnh của slide:
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()
Xuất Văn Bản Đoạn Thành HTML
Sử dụng ParagraphCollection.exportToHtml để xuất một phạm vi đoạn đã chọn dưới dạng HTML. Khi tái sử dụng văn bản bản trình bày trên website, bạn có thể muốn giữ nguyên nhấn mạnh cục bộ mà không mang theo phông, kích thước hoặc kiểu hộp văn bản của bản trình bày. Đặt TextToHtmlConversionOptions.setTextInheritanceLimit để kiểm soát mức độ bộ xuất tuân theo kế thừa định dạng.
Tải xuống bản trình bày mẫu và đặt nó trong thư mục làm việc của ứng dụng. Slide đầu tiên chứa một hình dạng văn bản với văn bản “Inherited text; explicit red 18pt”. Đầu vào cố ý sử dụng một số cấp định dạng:
- Kiểu văn bản mặc định của bản trình bày cung cấp phông Calibri và kích thước 28 pt.
- Kiểu văn bản cấp một của khung cung cấp màu xanh và in nghiêng.
- Đoạn văn đặt in đậm một cách rõ ràng.
- Phần thứ hai, “explicit red 18pt”, đặt màu đỏ và kích thước 18 pt một cách rõ ràng.
Ví dụ hoàn chỉnh sau đây xuất cùng các đoạn ba lần. Nó truyền TextToHtmlConversionOptions cho phương thức xuất và ghi mỗi kết quả thành một tệp HTML UTF‑8 riêng:
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()
HTML tạo ra chứa CSS sau cho phần đầu tiên, “Inherited text; “. Bảng liệt kê các khai báo liên quan; các thuộc tính được tạo khác bị bỏ qua để rõ ràng.
| Đầu ra | CSS liên quan cho phần đầu tiên | Hiển thị |
|---|---|---|
All.html |
font-family:'Calibri'; font-size:28pt; color:#0000ff; font-style:italic; font-weight:bold |
Văn bản màu xanh, in đậm, in nghiêng ở cỡ 28 pt trong phông Calibri, nếu phông đó có sẵn. |
TextBox.html |
color:#0000ff; font-style:italic; font-weight:bold |
Văn bản màu xanh, in đậm, in nghiêng; trang hoặc trình duyệt bên ngoài cung cấp phông chữ và kích thước. |
ParagraphOnly.html |
font-weight:bold |
Văn bản in đậm; trang hoặc trình duyệt bên ngoài cung cấp phông chữ, kích thước, màu sắc và kiểu in nghiêng. |
Phần thứ hai giữ nguyên color:#ff0000, font-size:18pt và font-weight:bold trong cả ba kết quả. Nó cũng giữ nguyên kiểu nghiêng với All và TextBox, nhưng không giữ với ParagraphOnly. Việc hạn chế kế thừa do đó không xóa bỏ định dạng được gán rõ ràng cho đoạn hoặc phần.
Mở các tệp trong cùng một trình duyệt để so sánh. Trong hai chế độ hạn chế, các thuộc tính CSS bị bỏ qua sẽ được giải quyết bởi kiểu của trang đích hoặc mặc định của trình duyệt, vì vậy giao diện cuối cùng phụ thuộc vào nơi HTML được sử dụng.
Tạo Fragment Clipboard HTML
Sử dụng TextToHtmlConversionOptions.setAddClipboardFragmentHeader để chọn định dạng đầu ra của ParagraphCollection.exportToHtml. Với False, phương thức trả về HTML thông thường. Với True, nó bổ sung một tiêu đề clipboard Windows. Các trường StartHTML và EndHTML cho biết vị trí byte UTF‑8 của HTML. Các trường StartFragment và EndFragment giới hạn nội dung cần dán.
Ví dụ này xuất hai đoạn đã định dạng dưới dạng HTML thông thường (html) và dưới dạng fragment clipboard HTML (clipboard_html). Một ứng dụng sau đó có thể ghi fragment này vào clipboard hệ thống. Nếu API clipboard yêu cầu byte thô, sử dụng clipboard_html.encode("utf-8") để mã hoá fragment mà không có BOM.
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()
Kết Xuất Đoạn Văn Thành Hình Ảnh
Paragraph.getImage kết xuất trực tiếp một đoạn riêng lẻ và trả về một đối tượng hình ảnh. Lưu kết quả vào tệp hoặc luồng bằng phương thức save. Bạn không cần phải kết xuất shape chứa hoặc cắt bitmap thủ công.
Paragraph.getImage có thể trả về None nếu đoạn không tồn tại trong bộ sưu tập cha, không có giới hạn kết xuất hợp lệ, hoặc không thể kết xuất. Kiểm tra kết quả trước khi lưu và giải phóng hình ảnh trả về sau khi dùng.
Kết Xuất Đoạn Văn ở Tỷ Lệ Mặc Định
Ví dụ sử dụng một bản trình bày có một slide, shape đầu tiên là một textbox chứa ba đoạn văn.

Ví dụ sau chọn đoạn thứ hai ở chỉ số 1 và gọi Paragraph.getImage mà không truyền tham số tỷ lệ. Nó kiểm tra shape có chứa đoạn cần thiết và việc kết xuất trả về hình ảnh trước khi gọi save để lưu dưới dạng PNG. Khối finally giải phóng hình ảnh sau khi dùng.
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()
Kết quả:

Kết Xuất Đoạn Văn Trong Ô Bảng Với Tỷ Lệ
Sử dụng phiên bản overload của Paragraph.getImage cho phép truyền các tham số scale_x và scale_y để đặt hệ số tỷ lệ ngang và dọc. Ví dụ sau tạo một bảng, kết xuất đoạn trong ô đầu tiên với độ rộng và chiều cao gấp đôi so với mặc định, và lưu kết quả dưới dạng 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()
Hệ số 1 giữ trục đó ở kích thước pixel mặc định. Ví dụ, 2 cho cả hai trục tạo ra hình ảnh có độ rộng và chiều cao khoảng gấp đôi kích thước mặc định, tương đương bốn lần số pixel. Các hệ số lớn hơn thường cho văn bản sắc nét hơn khi phóng to hoặc xuất ở độ phân giải cao, nhưng cũng làm tăng bộ nhớ và kích thước tệp. Các hệ số dưới 1 tạo hình ảnh nhỏ hơn với chi tiết ít hơn. Sử dụng các hệ số bằng nhau để giữ tỷ lệ khung hình của đoạn; các hệ số ngang và dọc khác nhau sẽ kéo giãn đầu ra độc lập.
Kết xuất toàn bộ shape bằng Shape.getImage vẫn hữu ích khi đầu ra cần bao gồm màu nền, viền hoặc ngữ cảnh hình ảnh khác của shape. Đối với ảnh chỉ chứa đoạn, hãy sử dụng Paragraph.getImage.
CÂU HỎI THƯỜNG GẶP
Có thể tắt hoàn toàn việc gói dòng bên trong một khung văn bản không?
Có. Đặt TextFrameFormat.setWrapText để tắt gói, để các dòng không ngắt tại cạnh của khung văn bản.
Làm thế nào để lấy tọa độ chính xác trên slide của một đoạn văn cụ thể?
Sử dụng Paragraph.getRect để lấy hình chữ nhật bao quanh đoạn. Portion.getRect cung cấp tọa độ của một phần riêng lẻ.
Ở đâu được kiểm soát căn chỉnh đoạn (trái, phải, giữa hoặc căn đều)?
ParagraphFormat.setAlignment là cài đặt cấp đoạn và áp dụng cho toàn bộ đoạn bất kể định dạng của các phần riêng lẻ.
Để căn dọc các phần có kích thước phông khác nhau trong mỗi dòng, xem Align Fonts Within a Line.
Có thể đặt ngôn ngữ kiểm tra chính tả cho một phần của đoạn không?
Có. Đặt BasePortionFormat.setLanguageId cho các phần riêng lẻ, để một đoạn có thể chứa văn bản bằng nhiều ngôn ngữ.