Open latheefS opened 5 months ago
for example using a self made method add_text()
with the necessary parameters and the code within it
`
if not paragraph:
paragraph: Paragraph = self.doc.add_paragraph()
style = self.doc.styles[heading_style]
paragraph.paragraph_format.space_after = Pt(space_after)
paragraph.paragraph_format.space_before = Pt(space_before)
paragraph.paragraph_format.keep_with_next = keep_with_next
paragraph.paragraph_format.alignment = halignment
paragraph.style = style
run = paragraph.add_run(text)
run.italic = italic
run.bold = bold
if font:
run.font.name = font
if heading_style == 'Normal' and font != 11:
run.font.size = Pt(font_size)
return paragraph
`
I've multiple paragraphs in which i need to make few words as bold and insert into word. For which i had tried the below method for a single paragraph.
import docx from docx import Document
start_bold = "\033[1m" end_bold = "\033[0m"
doc = docx.Document()
IntroPara = "This document details the results of the Quarterly Update Testing. As per Oracle’s timetable for quarterly releases for wave , the upgrade took place on non-production environments on the <day(dd/mm/yy)>, and into the production environment on the <day(dd/mm/yy)>."
words_to_highlight = ["", "", "<day(dd/mm/yy)>"]
for word in words_to_highlight: IntroPara = IntroPara.replace(word, f"{start_bold}{word}{end_bold}")
IntPara = IntroPara
print(IntPara) doc.add_paragraph(IntPara) doc.save('Report.docx')
this gives me highlted words but when i try to save it as a paragraph in word it is throwing below error.
### errors: This document details the results of the Quarterly Update Testing. As per Oracle’s timetable for quarterly releases for wave , the upgrade took place on non-production environments on the <day(dd/mm/yy)>, and into the production environment on the <day(dd/mm/yy)>.
Traceback (most recent call last):
File "C:\Users\TSTUSER\PycharmProjects\Test\parag.py", line 21, in
doc.add_paragraph(IntPara)
File "C:\Users\TSTUSER\PycharmProjects\test.venv\Lib\site-packages\docx\document.py", line 69, in add_paragraph
return self._body.add_paragraph(text, style)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\TSTUSER\PycharmProjects\test.venv\Lib\site-packages\docx\blkcntnr.py", line 55, in add_paragraph
paragraph.add_run(text)
File "C:\Users\TSTUSER\PycharmProjects\test.venv\Lib\site-packages\docx\text\paragraph.py", line 41, in add_run
run.text = text
^^^^^^^^
File "C:\Users\TSTUSER\PycharmProjects\test.venv\Lib\site-packages\docx\text\run.py", line 213, in text
self._r.text = text
^^^^^^^^^^^^
File "C:\Users\TSTUSER\PycharmProjects\test.venv\Lib\site-packages\docx\oxml\text\run.py", line 129, in text
_RunContentAppender.append_to_run_from_text(self, text)
File "C:\Users\TSTUSER\PycharmProjects\test.venv\Lib\site-packages\docx\oxml\text\run.py", line 248, in append_to_run_from_text
appender.add_text(text)
File "C:\Users\TSTUSER\PycharmProjects\test.venv\Lib\site-packages\docx\oxml\text\run.py", line 254, in add_text
self.flush()
File "C:\Users\TSTUSER\PycharmProjects\test.venv\Lib\site-packages\docx\oxml\text\run.py", line 275, in flush
self._r.add_t(text)
File "C:\Users\TSTUSER\PycharmProjects\test.venv\Lib\site-packages\docx\oxml\text\run.py", line 41, in add_t
t = self._add_t(text=text)
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\TSTUSER\PycharmProjects\test.venv\Lib\site-packages\docx\oxml\xmlchemy.py", line 300, in _add_child
setattr(child, key, value)
File "src\lxml\etree.pyx", line 1063, in lxml.etree._Element.text.set
File "src\lxml\apihelpers.pxi", line 749, in lxml.etree._setNodeText
File "src\lxml\apihelpers.pxi", line 737, in lxml.etree._createTextNode
File "src\lxml\apihelpers.pxi", line 1530, in lxml.etree._utf8
ValueError: All strings must be XML compatible: Unicode or ASCII, no NULL bytes or control characters
Please help me with a fix or alternate way to highlighted words inside the paragraph instead of addrun.