Answer the question
In order to leave comments, you need to log in
How to set paragraph indentation in docx python?
Good afternoon!
How to set a paragraph indent in a document generated using docx?
Well, I can not understand the documentation.
If possible, post a sample code.
first_line_indent was able to do. But this only works for one paragraph.
To use for three paragraphs - you need to enter three variables (based on the code that I got).
How can I do this for the entire document?
UPD. \t - helps. But the option of using the built-in features of the docx package is interesting. The document can be VERY multipage.
from docx import Document
from docx.shared import Inches
from docx.enum.text import WD_TAB_ALIGNMENT, WD_TAB_LEADER
document = Document()
p = document.add_paragraph('A plain paragraph having some ')
paragraph_format = p.paragraph_format
paragraph_format.first_line_indent = Inches(0.5)
tab_stops = paragraph_format.tab_stops
tab_stop = tab_stops.add_tab_stop(Inches(1.5), WD_TAB_ALIGNMENT.RIGHT, WD_TAB_LEADER.DOTS)
p.add_run('bold').bold = True
p.add_run(' и немного русского.')
s = document.add_paragraph(
'''a plain paragraph having some a plain paragraph having some a plain paragraph having some a plain paragraph having some a plain paragraph having some a plain paragraph having some a plain paragraph having some a plain paragraph having some ''')
paragraph_format = s.paragraph_format
paragraph_format.first_line_indent = Inches(0.5)
document.save('dem55.docx')
Answer the question
In order to leave comments, you need to log in
What if you use https://github.com/elapouya/python-docx-template/t...
I think this will not help you (3 years have passed), but I hope it will help
others
from docx import Document
from docx.shared import Inches, Pt
#_______1
section = document.sections[-1]
section.top_margin = Inches(0.8) #Верхний отступ
section.bottom_margin = Inches(0.8) #Нижний отступ
section.left_margin = Inches(1.2) #Отступ слева
section.right_margin = Inches(0.6) #Отступ справа
#_______2
paragraph_format = document.styles['Normal'].paragraph_format
paragraph_format.line_spacing = Pt(12) #межстрочный интервал
#_______3
style = document.styles['Normal']
font = style.font
font.name ='Times New Roman' #Стиль шрифта
font.size = Pt(12) #Размер шрифта
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question