Python में पाठ को कैसे स्वरूपित करें

Python में पाठ को कैसे स्वरूपित करें

Aspose.Slides FOSS for Python provides fine-grained text formatting through the PortionFormat class. A Portion पाठ की सबसे छोटी स्वतंत्र इकाई है; यह एक पैराग्राफ के भीतर एकल फ़ॉर्मेटिंग रन से मेल खाती है। यह गाइड दिखाता है कि प्रस्तुति में पाठ पर बोल्ड, इटैलिक, फ़ॉन्ट आकार, और रंग फ़ॉर्मेटिंग कैसे लागू की जाए।.

स्टेप बाय स्टेप गाइड

Step 1: पैकेज स्थापित करें

pip install aspose-slides-foss

चरण 2: टेक्स्ट फ्रेम के साथ एक शेप जोड़ें

फ़ॉर्मेटिंग करने से पहले, एक shape में एक TextFrame.उपयोग करें shape.add_text_frame() एक बनाने के लिए।.

import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    slide = prs.slides[0]
    shape = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 50, 50, 500, 150)
    tf = shape.add_text_frame("Default text: will be formatted")
    prs.save("output.pptx", SaveFormat.PPTX)

चरण 3: TextFrame तक पहुँचें

shape.add_text_frame() वापस देता है TextFrame ऑब्जेक्ट। आप इसे बाद में के माध्यम से भी प्राप्त कर सकते हैं shape.text_frame.

tf = shape.text_frame          # if the frame already exists
tf = shape.add_text_frame("") # creates a new frame

A TextFrame में एक सूची होती है Paragraph ऑब्जेक्ट्स (tf.paragraphs). प्रत्येक Paragraph शामिल करता है Portion ऑब्जेक्ट्स (paragraph.portions).


चरण 4: बोल्ड और इटैलिक फ़ॉर्मेटिंग लागू करें

उपयोग करें portion_format.font_bold और portion_format.font_italic. ये गुण स्वीकार करते हैं NullableBool.TRUE, NullableBool.FALSE, या NullableBool.NOT_DEFINED (मास्टर से विरासत में ले).

import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType, NullableBool
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    slide = prs.slides[0]
    shape = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 50, 50, 500, 150)
    tf = shape.add_text_frame("Bold and italic text")

    fmt = tf.paragraphs[0].portions[0].portion_format
    fmt.font_bold = NullableBool.TRUE
    fmt.font_italic = NullableBool.TRUE

    prs.save("bold-italic.pptx", SaveFormat.PPTX)

चरण 5: फ़ॉन्ट आकार और रंग सेट करें

सेट करें portion_format.font_height आकार (पॉइंट्स में) के लिए और उपयोग करें fill_format रंग के लिए।.

import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType, NullableBool, FillType
from aspose.slides_foss.drawing import Color
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    slide = prs.slides[0]
    shape = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 50, 50, 500, 150)
    tf = shape.add_text_frame("Large corporate-blue heading")

    fmt = tf.paragraphs[0].portions[0].portion_format
    fmt.font_height = 32                          # 32pt font
    fmt.font_bold = NullableBool.TRUE
    fmt.fill_format.fill_type = FillType.SOLID
    fmt.fill_format.solid_fill_color.color = Color.from_argb(255, 0, 70, 127)

    prs.save("colored-text.pptx", SaveFormat.PPTX)

Color.from_argb(alpha, red, green, blue) प्रत्येक चैनल के लिए 0–255 मान स्वीकार करता है।.


चरण 6: एक पैराग्राफ में कई Portions

एक पैराग्राफ में विभिन्न फ़ॉर्मेटिंग वाले कई भाग हो सकते हैं। एक नया जोड़ें Portion एक पैराग्राफ के portions संग्रह:

import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType, NullableBool, FillType
from aspose.slides_foss.drawing import Color
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    slide = prs.slides[0]
    shape = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 50, 50, 600, 100)
    tf = shape.add_text_frame("")  # start with empty frame

    paragraph = tf.paragraphs[0]

    # First portion: normal text
    portion1 = paragraph.portions[0]
    portion1.text = "Normal text followed by "
    portion1.portion_format.font_height = 20

    # Second portion: bold red text
    portion2 = slides.Portion()
    portion2.text = "bold red text"
    portion2.portion_format.font_height = 20
    portion2.portion_format.font_bold = NullableBool.TRUE
    portion2.portion_format.fill_format.fill_type = FillType.SOLID
    portion2.portion_format.fill_format.solid_fill_color.color = Color.from_argb(255, 200, 0, 0)
    paragraph.portions.add(portion2)

    prs.save("mixed-format.pptx", SaveFormat.PPTX)

सामान्य समस्याएँ और समाधान

रंग सेट करने के बाद भी टेक्स्ट काला दिखता है

सुनिश्चित करें fill_format.fill_type = FillType.SOLID रंग असाइन करने से पहले is set सेट किया जाना चाहिए। fill type सेट किए बिना, रंग परिवर्तन का कोई प्रभाव नहीं हो सकता।.

NullableBool.TRUE vs True

portion_format.font_bold अपेक्षा करता है NullableBool.TRUE, Python नहीं True. Python असाइन करना True एक अपवाद उठा सकता है TypeError या बाइंडिंग के आधार पर चुपचाप कुछ नहीं कर सकता।.

फ़ॉन्ट सहेजी गई फ़ाइल में दिखाई नहीं देता है

यह latin_font property Latin फ़ॉन्ट फ़ैमिली को सेट करता है। यदि सेट नहीं किया गया है, तो presentation theme फ़ॉन्ट का उपयोग किया जाता है। Custom fonts को embedded या viewing machine पर उपलब्ध होना चाहिए।.


अक्सर पूछे जाने वाले प्रश्न

फ़ॉन्ट फ़ैमिली कैसे बदलें?

सेट portion_format.latin_font:

fmt.latin_font = slides.FontData("Arial")

FontData font family नाम को string के रूप में स्वीकार करता है।.

पैराग्राफ अलाइनमेंट कैसे सेट करें?

उपयोग करें paragraph_format.alignment:

from aspose.slides_foss import TextAlignment

tf.paragraphs[0].paragraph_format.alignment = TextAlignment.CENTER

समर्थित मान: LEFT, CENTER, RIGHT, JUSTIFY.

लाइन स्पेसिंग कैसे सेट करें?

उपयोग करें paragraph_format.space_before (पैराग्राफ से पहले बिंदु) या paragraph_format.space_after (पैराग्राफ के बाद बिंदु):

tf.paragraphs[0].paragraph_format.space_before = 12   # 12pt before
tf.paragraphs[0].paragraph_format.space_after = 6     # 6pt after

संबंधित देखें

 हिन्दी