This should do the job:
p.add_run('Strike through the following text').font.strike = True
If multiple font properties were to be changed the code should be :
sentence = p.add_run('Strike through the following text')
sentence.font.strike = True
sentence.font.name = 'Comic Sans MS'
strike is a property of the font object anycodings_python : docs,My problem is that when I apply the anycodings_python-docx strike-through or double strike-through anycodings_python-docx formatting and save the file it is not anycodings_python-docx reflected in output file.,This should do the job:,If multiple font properties were to be anycodings_python changed the code should be :
Following code does not do the trick:
from docx
import Document
document = Document()
p = document.add_paragraph()
p.add_run('Strike through the following text').strike = True
document.save('demo.docx')
This should do the job:
p.add_run('Strike through the following text').font.strike = True
If multiple font properties were to be anycodings_python changed the code should be :
sentence = p.add_run('Strike through the following text')
sentence.font.strike = True
sentence.font.name = 'Comic Sans MS'
A block-level item flows the text it contains between its left and right edges, adding an additional line each time the text extends beyond its right boundary. For a paragraph, the boundaries are generally the page margins, but they can also be column boundaries if the page is laid out in columns, or cell boundaries if the paragraph occurs inside a table cell.,The attributes of a block-level item specify its placement on the page, such items as indentation and space before and after a paragraph. The attributes of an inline item generally specify the font in which the content appears, things like typeface, font size, bold, and italic.,To work effectively with text, it’s important to first understand a little about block-level elements like paragraphs and inline-level objects like runs.,A paragraph has a variety of properties that specify its placement within its container (typically a page) and the way it divides its content into separate lines.
>>> from docx.enum.text import WD_ALIGN_PARAGRAPH >>> document = Document() >>> paragraph = document.add_paragraph() >>> paragraph_format = paragraph.paragraph_format >>> paragraph_format.alignment None # indicating alignment is inherited from the style hierarchy >>> paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER >>> paragraph_format.alignment CENTER(1)
>>> from docx.shared import Inches >>> paragraph = document.add_paragraph() >>> paragraph_format = paragraph.paragraph_format >>> paragraph_format.left_indent None # indicating indentation is inherited from the style hierarchy >>> paragraph_format.left_indent = Inches(0.5) >>> paragraph_format.left_indent 457200 >>> paragraph_format.left_indent.inches 0.5
>>> from docx.shared
import Pt
>>>
paragraph_format.right_indent
None
>>>
paragraph_format.right_indent = Pt(24) >>>
paragraph_format.right_indent
304800
>>>
paragraph_format.right_indent.pt
24.0
>>> paragraph_format.first_line_indent None >>> paragraph_format.first_line_indent = Inches(-0.25) >>> paragraph_format.first_line_indent - 228600 >>> paragraph_format.first_line_indent.inches - 0.25
>>> tab_stops = paragraph_format.tab_stops
>>> tab_stops
<docx.text.tabstops.TabStops object at 0x106b802d8>
>>> tab_stop = tab_stops.add_tab_stop(Inches(1.5)) >>> tab_stop.position 1371600 >>> tab_stop.position.inches 1.5
Last Updated : 03 Jan, 2021,GATE CS 2021 Syllabus
Word documents contain formatted text wrapped within three object levels. The Lowest level-run objects, middle level-paragraph objects, and highest level-document object. So, we cannot work with these documents using normal text editors. But, we can manipulate these word documents in python using the python-docx module. Pip command to install this module is:
pip install python - docx
To set the text to bold you have to set it true.
doc.bold = True
To highlight a specific word the bold needs to be set True along with its add_run() statement.
add_run(" text ").bold = True
To make some specific word(s) italics, it needs to be set True along with its add_run() statement.
add_run(" text ").italic = True
To apply to underline to a text you have to set it true.
doc.underline = True