fromdocximportDocumentfromdocx.sharedimportPtfromdocx.oxmlimportOxmlElementdefset_line_spacing(paragraph,spacing):"""设置段落的行高"""# 使用 OxmlElement 创建 XML 元素p=paragraph._element pPr=p.get_or_add_pPr()spacing_element=OxmlElement('w:spacing')spacing_element.set('w:line',str(spacing))...
# line_spacing,line_spacing_rule 行距 # paragraph_format=paragraph.paragraph_format # paragraph_format.space_before=Pt(0) #上行间距 # paragraph_format.space_after=Pt(0) #下行间距 # paragraph_format.line_spacing=1.15 #行距 # from docx.enum.text import WD_LINE_SPACING # ONE_POINT_FIVE,1.5...
paragraph.paragraph_format.space_before = Pt(10)# 0.5行(10磅)paragraph.paragraph_format.space_after = Pt(10)# 0.5行(10磅)paragraph.paragraph_format.line_spacing = Pt(20)# 设置序号与标题之间空一格run.text = run.text.split(' ')[0] +' '+' '.join(run.text.split(' ')[1:])defgener...
from docx.oxml.ns import qn from docx.shared import Pt document = Document() paragraph1 = document.add_paragraph() # paragraph.line_spacing_rule = WD_LINE_SPACING.EXACTLY # 行距固定值 # paragraph.line_spacing_rule = WD_LINE_SPACING.MULTIPLE # 多倍行距 # paragraph1.paragraph_format.line_sp...
document.save('test.docx') 其它案例 from docx import Document from docx.shared import Inches document = Document() # 0,标题:一、关于图片 document.add_heading('一、关于图片', level=2) # 1,添加图片 document.add_paragraph('1,添加图片') ...
方法/步骤 1 导入相关的库from docx import Documentfrom docx.shared import Pt 2 打开一个基于默认“模板”的空白文档document = Document()3 新增加一段段落paragraph = document.add_paragraph('这是一个新的段落')4 设置段落间距paragraph_format=paragraph.paragraph_formatparagraph_format.space_...
first_line_indent = paragraph_format.first_line_indent print('段落首行缩进:', first_line_indent) # 2.2.4 行间距 line_spacing = paragraph_format.line_spacing print('段落行间距:', line_spacing) # 2.2.5 段落前后间距 space_before, space_after = paragraph_format.space_before, paragraph_format...
myDocument.save('我的Word文件-散文名篇.docx') 03 代码说明 在上面这段代码中,myDocument.paragraphs[0].paragraph_format.line_spacing= docx.shared.Pt(16)表示设置Word文件(myDocument)的第1个段落的行间距为16磅,但是从图236-2的实际效果看,第1个段落的行间距几乎为0,为什么呢?因为第1个段落的文本是三...
在段落设置中,间距包括段前间距、段后间距和行间距。分别对应于了python-docx包docx.text.parfmt.ParagraphFormat中的space_before,space_after和line_spacing和line_spacing_rule。其中前三者为Length类型,同样需要从docx.shared类中导入单位。段前间距 段前间距使用space_before,默认值为None,代表从样式中继承。其...
doc=Document()#创建一个空白文档p=doc.add_paragraph()#给文档增加一个段落p.paragraph_format.space_before=Pt(0)#设置段落 段前 0 磅p.paragraph_format.space_after=Pt(0)#设置段落 段后 0 磅p.paragraph_format.line_spacing=1.5#设置该段落 行间距为 1.5倍#p.paragraph_format.first_line_indent=Inch...