[文档]classParagraph(StoryChild):"""Proxy object wrapping a `<w:p>` element."""def__init__(self,p:CT_P,parent:t.ProvidesStoryPart):super(Paragraph,self).__init__(parent)self._p=self._element=p
[文档]defadd_run(self,text:str|None=None,style:str|CharacterStyle|None=None)->Run:"""Append run containing `text` and having character-style `style`. `text` can contain tab (``\\t``) characters, which are converted to the appropriate XML form for a tab. `text` can also include newline (``\\n``) or carriage return (``\\r``) characters, each of which is converted to a line break. When `text` is `None`, the new run is empty. """r=self._p.add_r()run=Run(r,self)iftext:run.text=textifstyle:run.style=stylereturnrun
@propertydefalignment(self)->WD_PARAGRAPH_ALIGNMENT|None:"""A member of the :ref:`WdParagraphAlignment` enumeration specifying the justification setting for this paragraph. A value of |None| indicates the paragraph has no directly-applied alignment value and will inherit its alignment value from its style hierarchy. Assigning |None| to this property removes any directly-applied alignment value. """returnself._p.alignment@alignment.setterdefalignment(self,value:WD_PARAGRAPH_ALIGNMENT):self._p.alignment=value
[文档]defclear(self):"""Return this same paragraph after removing all its content. Paragraph-level formatting, such as style, is preserved. """self._p.clear_content()returnself
@propertydefcontains_page_break(self)->bool:"""`True` when one or more rendered page-breaks occur in this paragraph."""returnbool(self._p.lastRenderedPageBreaks)@propertydefhyperlinks(self)->List[Hyperlink]:"""A |Hyperlink| instance for each hyperlink in this paragraph."""return[Hyperlink(hyperlink,self)forhyperlinkinself._p.hyperlink_lst]
[文档]definsert_paragraph_before(self,text:str|None=None,style:str|ParagraphStyle|None=None)->Paragraph:"""Return a newly created paragraph, inserted directly before this paragraph. If `text` is supplied, the new paragraph contains that text in a single run. If `style` is provided, that style is assigned to the new paragraph. """paragraph=self._insert_paragraph_before()iftext:paragraph.add_run(text)ifstyleisnotNone:paragraph.style=stylereturnparagraph
[文档]defiter_inner_content(self)->Iterator[Run|Hyperlink]:"""Generate the runs and hyperlinks in this paragraph, in the order they appear. The content in a paragraph consists of both runs and hyperlinks. This method allows accessing each of those separately, in document order, for when the precise position of the hyperlink within the paragraph text is important. Note that a hyperlink itself contains runs. """forr_or_hlinkinself._p.inner_content_elements:yield(Run(r_or_hlink,self)ifisinstance(r_or_hlink,CT_R)elseHyperlink(r_or_hlink,self))
@propertydefparagraph_format(self):"""The |ParagraphFormat| object providing access to the formatting properties for this paragraph, such as line spacing and indentation."""returnParagraphFormat(self._element)@propertydefrendered_page_breaks(self)->List[RenderedPageBreak]:"""All rendered page-breaks in this paragraph. Most often an empty list, sometimes contains one page-break, but can contain more than one is rare or contrived cases. """return[RenderedPageBreak(lrpb,self)forlrpbinself._p.lastRenderedPageBreaks]@propertydefruns(self)->List[Run]:"""Sequence of |Run| instances corresponding to the <w:r> elements in this paragraph."""return[Run(r,self)forrinself._p.r_lst]@propertydefstyle(self)->ParagraphStyle|None:"""Read/Write. |_ParagraphStyle| object representing the style assigned to this paragraph. If no explicit style is assigned to this paragraph, its value is the default paragraph style for the document. A paragraph style name can be assigned in lieu of a paragraph style object. Assigning |None| removes any applied style, making its effective value the default paragraph style for the document. """style_id=self._p.stylestyle=self.part.get_style(style_id,WD_STYLE_TYPE.PARAGRAPH)returncast(ParagraphStyle,style)@style.setterdefstyle(self,style_or_name:str|ParagraphStyle|None):style_id=self.part.get_style_id(style_or_name,WD_STYLE_TYPE.PARAGRAPH)self._p.style=style_id@propertydeftext(self)->str:"""The textual content of this paragraph. The text includes the visible-text portion of any hyperlinks in the paragraph. Tabs and line breaks in the XML are mapped to ``\\t`` and ``\\n`` characters respectively. Assigning text to this property causes all existing paragraph content to be replaced with a single run containing the assigned text. A ``\\t`` character in the text is mapped to a ``<w:tab/>`` element and each ``\\n`` or ``\\r`` character is mapped to a line break. Paragraph-level formatting, such as style, is preserved. All run-level formatting, such as bold or italic, is removed. """returnself._p.text@text.setterdeftext(self,text:str|None):self.clear()self.add_run(text)def_insert_paragraph_before(self):"""Return a newly created paragraph, inserted directly before this paragraph."""p=self._p.add_p_before()returnParagraph(p,self._parent)