对齐样式
paragraph.alignment = 对齐方式
| 对齐方式 |
说明 |
| LEFT |
靠左 |
| CENTER |
居中 |
| RIGHT |
靠右 |
| JUSTIFY |
两端对齐 |
| DISTRIBUTE |
分散对齐 |
| JUSTIFY_MED |
以中等字符压缩比调整 |
| JUSTIFY_HI |
以高字符压缩比调整 |
| JUSTIFY_LOW |
以低字符压缩比调整 |
| THAI_JUSTIFY |
以泰语格式调整 |
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx import Document
文件 = Document('c:/test.docx')
for 段落 in 文件.paragraphs:
if 段落.style.name=='Normal':
段落.alignment = WD_ALIGN_PARAGRAPH.CENTER
文件.save('c:/test.docx')
行间距
正文之间的行间距
from docx import Document
文件 = Document('c:/test.docx')
for 段落 in 文件.paragraphs:
if 段落.style.name=='Normal':
段落.paragraph_format.line_spacing = 5.0
文件.save('c:/test.docx')
全部段落之间的行间距
from docx import Document
文件 = Document('c:/test.docx')
for 段落 in 文件.paragraphs:
段落.paragraph_format.line_spacing = 5.0
文件.save('c:/test.docx')
特殊行间距调整
| 参数 |
说明 |
参数 |
说明 |
| ONE_POINT_FIVE |
1.5倍行距 |
AT_LEAST |
最小行距 |
| DOUBLE |
双倍行距 |
EXACTLY |
固定值 |
| MULTIPLE |
多倍行距 |
SINGL |
单倍行距 |
1.5倍行距
from docx.enum.text import WD_LINE_SPACING
from docx import Document
文件 = Document('c:/test.docx')
for 段落 in 文件.paragraphs:
段落.paragraph_format.line_spacing_rule = WD_LINE_SPACING.ONE_POINT_FIVE
print(段落.paragraph_format.line_spacing)
文件.save('c:/test.docx')
固定值
from docx.enum.text import WD_LINE_SPACING
from docx import Document
from docx.shared import Pt
文件 = Document('c:test.docx')
for 段落 in 文件.paragraphs:
段落.line_spacing_rule = WD_LINE_SPACING.EXACTLY
段落.paragraph_format.line_spacing = Pt(18)
文件.save('c:/test.docx')
多倍行距
from docx.enum.text import WD_LINE_SPACING
from docx import Document
文件 = Document('c:/test.docx')
for 段落 in 文件.paragraphs:
段落.line_spacing_rule = WD_LINE_SPACING.MULTIPLE
段落.paragraph_format.line_spacing = 1.75
文件.save('c:/test.docx')
段前与段后间距
全部设置
from docx import Document
from docx.shared import Pt, Inches # Pt磅,Inches英寸
文件 = Document('c:/test.docx')
for 段落 in 文件.paragraphs:
段落.paragraph_format.space_before = Pt(18) # 段前
段落.paragraph_format.space_after = Pt(12) # 段后
文件.save('c:/test.docx')
设置正文部分
from docx import Document
from docx.shared import Pt, Inches # Pt磅,Inches英寸
文件 = Document('c:/test.docx')
for 段落 in 文件.paragraphs:
if 段落.style.name == 'Normal':
段落.paragraph_format.space_before = Pt(18) # 段前
段落.paragraph_format.space_after = Pt(12) # 段后
文件.save('c:/test.docx')
缩进
| 参数 |
说明 |
数据类型 |
| pt |
磅 |
int |
| cm |
厘米 |
float |
| inches |
英寸 |
float |
| mm |
毫米 |
float |
左缩进
from docx import Document
from docx.shared import Pt, Inches # Pt磅,Inches英寸
文件 = Document('c:/test.docx')
for 段落 in 文件.paragraphs:
if 段落.style.name == 'Normal':
段落.paragraph_format.left_indent=Inches(0.3) # 1 英寸=2.54 厘米
文件.save('c:/test.docx')
右缩进
from docx import Document
from docx.shared import Pt, Inches # Pt磅,Inches英寸
文件 = Document('c:/test.docx')
for 段落 in 文件.paragraphs:
if 段落.style.name == 'Normal':
段落.paragraph_format.right_indent=Inches(0.3) # 1 英寸=2.54 厘米
文件.save('c:/test.docx')
首行缩进
正数是首行缩进,负数是悬挂缩进,Inches(0.5)等于四个空格
from docx import Document
from docx.shared import Pt, Inches # Pt磅,Inches英寸
文件 = Document('c:/test.docx')
for 段落 in 文件.paragraphs:
if 段落.style.name == 'Normal':
段落.paragraph_format.first_line_indent=Inches(0.3) # 1 英寸=2.54 厘米
文件.save('c:/test.docx')