Zarządzanie akapitami tekstu PowerPoint w Pythonie za pośrednictwem Java

Przegląd

Aspose.Slides for Python via Java przedstawia tekst jako hierarchię ramek tekstowych, akapitów i fragmentów:

  • TextFrame reprezentuje kontener tekstu w kształcie i zapewnia dostęp do jego kolekcji akapitów.
  • Paragraph reprezentuje pojedynczy akapit w ramce tekstowej i zapewnia dostęp do jego fragmentów oraz formatowania na poziomie akapitu.
  • Portion reprezentuje fragment tekstu w akapicie. Każdy fragment może mieć własny tekst i formatowanie na poziomie znaków.

Akapit może więc zawierać tekst z różnymi czcionkami, kolorami, rozmiarami i innym formatowaniem, używając wielu fragmentów.

Tworzenie i formatowanie akapitów

Tworzenie akapitów z wieloma fragmentami

Poniższe kroki tworzą ramkę tekstową z trzema akapitami, z których każdy zawiera trzy fragmenty:

  1. Utwórz instancję klasy Presentation.
  2. Uzyskaj dostęp do odpowiedniego slajdu przez jego indeks.
  3. Dodaj prostokątną AutoShape do slajdu.
  4. Uzyskaj dostęp do TextFrame kształtu.
  5. Użyj domyślnego akapitu i dodaj dwa kolejne obiekty Paragraph do ramki tekstowej.
  6. Dodaj wystarczającą liczbę obiektów Portion do każdego akapitu, aby zawierały po trzy fragmenty. Domyślny akapit już zawiera jeden pusty fragment.
  7. Ustaw tekst każdego fragmentu.
  8. Zastosuj formatowanie na poziomie znaków poprzez Portion.getPortionFormat.
  9. Zapisz zmodyfikowaną prezentację.

Ten przykład w Pythonie realizuje powyższe kroki:

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

Tworzenie list wypunktowanych i numerowanych

Tworzenie listy wypunktowanej lub numerowanej

Wypunktowania i numerowanie ułatwiają przeglądanie powiązanych elementów. W Aspose.Slides ustawienia listy definiowane są przy pomocy BulletFormat.

  1. Utwórz instancję klasy Presentation.
  2. Uzyskaj dostęp do odpowiedniego slajdu przez jego indeks.
  3. Dodaj AutoShape do wybranego slajdu.
  4. Uzyskaj dostęp do TextFrame kształtu.
  5. Usuń domyślny akapit z ramki tekstowej.
  6. Utwórz Paragraph dla wypunktowania symbolicznego.
  7. Ustaw BulletFormat.setType na BulletType.Symbol i określ znak wypunktowania.
  8. Ustaw tekst akapitu, wcięcie, kolor wypunktowania i wysokość wypunktowania.
  9. Dodaj akapit do ramki tekstowej.
  10. Utwórz drugi akapit i ustaw BulletFormat.setType na BulletType.Numbered.
  11. Skonfiguruj styl numerowanego wypunktowania i dodaj akapit do ramki tekstowej.
  12. Zapisz prezentację.

Ten przykład w Pythonie tworzy wypunktowanie symboliczne i numerowane:

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

Używanie wypunktowania obrazkowego

Wypunktowanie obrazkowe pozwala użyć własnego obrazu zamiast symbolu lub liczby.

  1. Utwórz instancję klasy Presentation.
  2. Uzyskaj dostęp do odpowiedniego slajdu przez jego indeks.
  3. Dodaj AutoShape i uzyskaj dostęp do jego TextFrame.
  4. Usuń domyślny akapit z ramki tekstowej.
  5. Wczytaj obraz wypunktowania i dodaj go do kolekcji obrazów prezentacji jako PPImage.
  6. Utwórz Paragraph i ustaw jego tekst.
  7. Ustaw BulletFormat.setType na BulletType.Picture.
  8. Przypisz obraz poprzez BulletFormat.getPicture i ustaw wysokość wypunktowania.
  9. Dodaj akapit do ramki tekstowej.
  10. Zapisz zmodyfikowaną prezentację.

Ten przykład w Pythonie tworzy wypunktowanie obrazkowe:

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

Tworzenie listy wielopoziomowej

Ustaw ParagraphFormat.setDepth, aby umieścić akapity na różnych poziomach listy. Poziom najwyższy ma głębokość 0.

  1. Utwórz Presentation i uzyskaj dostęp do slajdu.
  2. Dodaj AutoShape i wyczyść domyślny akapit z jego ramki tekstowej.
  3. Utwórz cztery akapity i skonfiguruj ich symbole wypunktowań.
  4. Ustaw ich wartości ParagraphFormat.setDepth na 0, 1, 2 i 3.
  5. Dodaj akapity do ramki tekstowej i zapisz prezentację.

Ten przykład w Pythonie tworzy czteropoziomową listę wypunktowaną:

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

Rozpoczęcie numerowanej listy od niestandardowych wartości

Użyj BulletFormat.setNumberedBulletStartWith, aby ustawić początkowy numer wyświetlany dla numerowanego akapitu.

  1. Utwórz Presentation i dodaj AutoShape do slajdu.
  2. Wyczyść domyślny akapit z ramki tekstowej kształtu.
  3. Utwórz trzy numerowane akapity.
  4. Ustaw BulletFormat.setNumberedBulletStartWith na 2, 3 i 7 dla kolejnych akapitów.
  5. Dodaj akapity do ramki tekstowej i zapisz prezentację.

Ten przykład w Pythonie przypisuje niestandardowy numer początkowy każdemu akapitowi:

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

Kontrola układu akapitu i właściwości końcowych

Ustawienie wcięcia pierwszej linii

Użyj ParagraphFormat.setIndent, aby kontrolować wcięcie pierwszej linii akapitu. Metoda ta przesuwa tylko pierwszą linię względem lewego marginesu akapitu. Dodatnia wartość przesuwa pierwszą linię w prawo, a pozostałe linie pozostają wyrównane do treści akapitu.

Użyj ParagraphFormat.setMarginLeft, gdy potrzebujesz przesunąć cały akapit. Użyj ParagraphFormat.setIndent, gdy chcesz przesunąć tylko pierwszą linię.

Poniższy przykład tworzy kilka akapitów i stosuje różne wartości [ParagraphFormat.setIndent], aby zilustrować, jak wcięcie pierwszej linii wpływa na układ akapitu.

  1. Utwórz instancję klasy Presentation.
  2. Uzyskaj dostęp do docelowego slajdu.
  3. Dodaj prostokątną AutoShape do slajdu.
  4. Uzyskaj dostęp do TextFrame kształtu i usuń domyślny akapit.
  5. Utwórz kilka akapitów i ustaw różne wartości [ParagraphFormat.setIndent] dla nich.
  6. Dodaj akapity do ramki tekstowej.
  7. Zapisz zmodyfikowaną prezentację.

Ten kod pokazuje, jak ustawić wcięcie akapitu:

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

Wynik:

Wcięcie pierwszej linii akapitów

Ustawienie wcięcia wiszącego

Wcięcie wiszące to układ akapitu, w którym pierwsza linia zaczyna się lewiej niż pozostałe linie. W Aspose.Slides tworzysz ten efekt przy pomocy ParagraphFormat.setIndent. Przekaż ujemną wartość, aby przesunąć pierwszą linię w lewo względem treści akapitu.

W praktyce ParagraphFormat.setMarginLeft definiuje lewą pozycję treści akapitu, a ParagraphFormat.setIndent definiuje pozycję pierwszej linii względem tego marginesu. Aby uzyskać wcięcie wiszące, podaj dodatnią wartość do [ParagraphFormat.setMarginLeft] i ujemną do [ParagraphFormat.setIndent].

To formatowanie jest przydatne w bibliografiach, odnośnikach, hasłach słowników i innych akapitach, w których zawinięte linie muszą wyrównywać się pod treścią akapitu, a nie pod pierwszym znakiem pierwszej linii.

  1. Utwórz instancję klasy Presentation.
  2. Uzyskaj dostęp do docelowego slajdu.
  3. Dodaj prostokątną AutoShape do slajdu.
  4. Uzyskaj dostęp do TextFrame kształtu i usuń domyślny akapit.
  5. Utwórz akapity i podaj dodatnią wartość do [ParagraphFormat.setMarginLeft] dla każdego akapitu.
  6. Podaj ujemną wartość do [ParagraphFormat.setIndent], aby uzyskać efekt wcięcia wiszącego.
  7. Dodaj akapity do ramki tekstowej.
  8. Zapisz zmodyfikowaną prezentację.

Ten kod pokazuje, jak ustawić wcięcie wiszące dla akapitu:

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

Wynik:

Wcięcie wiszące akapitów

Ustawienie właściwości końcowych akapitu

Paragraph.setEndParagraphPortionFormat kontroluje formatowanie znacznika końca akapitu. Poniższy przykład przypisuje rozmiar czcionki i czcionkę łacińską do znacznika końcowego drugiego akapitu:

  1. Załaduj Presentation i uzyskaj dostęp do slajdu.
  2. Dodaj AutoShape i wyczyść jego domyślny akapit.
  3. Utwórz dwa akapity i dodaj do nich fragmenty tekstu.
  4. Utwórz PortionFormat dla znacznika końcowego drugiego akapitu.
  5. Ustaw BasePortionFormat.setFontHeight oraz BasePortionFormat.setLatinFont.
  6. Przypisz format za pomocą Paragraph.setEndParagraphPortionFormat i zapisz prezentację.
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()

Import i eksport treści akapitu

Importowanie tekstu HTML do akapitów

Użyj ParagraphCollection.addFromHtml, aby skonwertować znacznik HTML na akapity i fragmenty w ramce tekstowej.

  1. Utwórz instancję klasy Presentation.
  2. Uzyskaj dostęp do slajdu i dodaj AutoShape.
  3. Uzyskaj dostęp do TextFrame kształtu i wyczyść jego domyślny akapit.
  4. Odczytaj plik źródłowego HTML.
  5. Przekaż ciąg HTML do ParagraphCollection.addFromHtml.
  6. Zapisz zmodyfikowaną prezentację.

Ten przykład w Pythonie importuje HTML do ramki tekstowej:

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

Eksport tekstu akapitu do HTML

Użyj ParagraphCollection.exportToHtml, aby wyeksportować wybrany zakres akapitów jako HTML.

  1. Utwórz instancję klasy Presentation i załaduj żądaną prezentację.
  2. Uzyskaj dostęp do slajdu i znajdź AutoShape, który zawiera tekst.
  3. Uzyskaj dostęp do TextFrame kształtu.
  4. Wywołaj ParagraphCollection.exportToHtml podając indeks początkowego akapitu oraz liczbę akapitów do wyeksportowania.
  5. Zapisz zwrócony ciąg HTML do pliku.

Ten przykład w Pythonie eksportuje wszystkie akapity z pierwszego kształtu tekstowego:

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

Renderowanie akapitu jako obrazu

Paragraph.getImage renderuje pojedynczy akapit bezpośrednio i zwraca obiekt obrazu. Zapisz wynik do pliku lub strumienia przy użyciu metody save. Nie musisz renderować całego kształtu ani ręcznie przycinać bitmapy.

Paragraph.getImage może zwrócić None, jeśli akapit nie zostanie znaleziony w kolekcji nadrzędnej, nie ma prawidłowych granic renderowania lub nie może być renderowany. Sprawdź wynik przed zapisem i zwolnij zwrócony obraz po użyciu.

Renderowanie akapitu w domyślnej skali

Załóżmy, że mamy plik prezentacji o nazwie sample.pptx z jednym slajdem, w którym pierwszy kształt to pole tekstowe zawierające trzy akapity.

Pole tekstowe z trzema akapitami

Poniższy przykład renderuje drugi akapit w zwykłym kształcie tekstowym w domyślnej skali i zapisuje zwrócony obraz w formacie PNG. Blok finally zapewnia prawidłowe zwolnienie obrazu.

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

Wynik:

Obraz akapitu

Renderowanie akapitu w komórce tabeli ze skalowaniem

Użyj przeciążenia Paragraph.getImage, które przyjmuje parametry scale_x i scale_y, aby ustawić czynniki skali poziomej i pionowej. Poniższy przykład tworzy tabelę, renderuje akapit w jej pierwszej komórce przy dwukrotnym zwiększeniu domyślnej szerokości i wysokości oraz zapisuje wynik jako obraz 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()

Czynnik skali 1 utrzymuje dany oś w domyślnym rozmiarze pikseli. Na przykład 2 dla obu czynników powoduje powstanie obrazu, którego szerokość i wysokość są w przybliżeniu dwukrotnie większe niż domyślne wymiary, co daje czterokrotnie więcej pikseli. Większe czynniki zazwyczaj dają ostrzejszy tekst przy powiększaniu lub wyjściu wysokiej rozdzielczości, ale zwiększają zużycie pamięci i rozmiar pliku. Czynniki poniżej 1 tworzą mniejsze obrazy z mniejszą szczegółowością. Używaj równych czynników, aby zachować proporcje akapitu; różne czynniki poziome i pionowe rozciągają wyjście niezależnie.

Renderowanie całego kształtu przy pomocy Shape.getImage pozostaje przydatne, gdy wynik musi zawierać wypełnienie, obramowanie lub inny kontekst wizualny kształtu. Dla obrazu wyłącznie akapitu użyj Paragraph.getImage.

FAQ

Czy mogę całkowicie wyłączyć łamanie linii wewnątrz ramki tekstowej?

Tak. Ustaw TextFrameFormat.setWrapText, aby wyłączyć łamanie, więc linie nie będą się dzielić na krawędziach ramki tekstowej.

Jak mogę uzyskać dokładne granice na slajdzie konkretnego akapitu?

Użyj Paragraph.getRect, aby pobrać prostokąt otaczający akapit. Portion.getRect dostarcza granice pojedynczego fragmentu.

Gdzie kontrolowane jest wyrównanie akapitu (lewo, prawo, środek lub wyjustowanie)?

ParagraphFormat.setAlignment jest ustawieniem na poziomie akapitu i ma zastosowanie do całego akapitu, niezależnie od formatowania poszczególnych fragmentów.

Czy mogę ustawić język korekty dla części akapitu?

Tak. Ustaw BasePortionFormat.setLanguageId dla poszczególnych fragmentów, aby jeden akapit mógł zawierać tekst w wielu językach.