T
T
tayoboc2017-01-24 16:36:07
Python
tayoboc, 2017-01-24 16:36:07

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

2 answer(s)
L
Larisa .•º, 2017-01-24
@barolina

What if you use https://github.com/elapouya/python-docx-template/t...

H
Hassle, 2021-01-29
@Hassle

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) #Размер шрифта

1. Entering indents "exact" and entering numbers can lead to a stupor, but don't worry, now I'll show everything in the image. Each division on the ruler is exactly 0.1 For example, 1.2 will be exactly 12 divisions (see image)
601349b08a726465888498.png
2. The line spacing is set up very simply, the choice of spacing format depends on the format of the entered number
Examples:
Pt(12) - Exact format
Inches(12) - Also exact format , but it's better not to use it, because by entering the number 12 we will get 864 pt at the output, so it's better to use Pt
(
) as well as line spacing
Hope this helps you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question