Search code examples
python-3.xpython-docx

change font direction in word with python3


I want to get a text from a word doc and make some change to it and put it into a new word doc

how can I change direction to Right-to-Lift and font size to 35

from docx import Document
doc = Document("nameOfTheFile.docx")
doc2 = Document()
n = 0
for p in doc.paragraphs:
    n += 1
    # doc2.add_paragraph(p.text)
    print(n)
doc2.save("nameOfTheFileEdited.docx")

Solution

  • You can work using this -

    Also refer -

    import docx
    from docx.shared import Pt
    
    doc = docx.Document('file_name.docx')
    doc1 = docx.Document()
    style = doc1.styles['Normal']
    font = style.font
    font.rtl = True
    font.size = Pt(35)
    for paragraph in doc.paragraphs:
        parag = doc1.add_paragraph()
        parag.add_run(paragraph.text)
        
    
    doc1.save('new_file.docx')