【PYTHON,WORD】3.调整Word文档样式
1.修改文字样式
from docx import Document
from docx.shared import Pt,RGBColor
from docx.oxml.ns import qndoc = Document("0.docx")
for paragraph in doc.paragraphs:for run in paragraph.runs:run.font.bold = True #黑体run.font.italic = True #斜体run.font.underline = True #下划线run.font.strike = True #删除线run.font.shadow = True #阴影run.font.size = Pt(15) #字体大小run.font.color.rgb = RGBColor(255,255,0) #颜色run.font.name = "微软雅黑" #字体r = run._element.rPr.rFonts #中文字体r.set(qn('w:eastAsia'),"微软雅黑")doc.save("xin0.docx")
2.修改段落格式
2.1对齐样式:
from docx.enum.text import WD_ALIGN_PARAGRAPHparagraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
也可选择其他不同对齐
如:LEFT,RIGHT等
2.2行间距
paragraph.paragraph_foramt.line_spacing = 2.0
paragraph.paragraph_foramt.line_spacing = 2.0
用浮点数,表示两倍行间距
2.3段前和段后间距
paragraph.paragraph_format.space_before = Pt(12)
paragraph.paragraph_format.space_after = Pt(21)
pt(21)表示21磅
3.整体代码
from docx import Document
from docx.shared import Pt,RGBColor
from docx.oxml.ns import qn
from docx.enum.text import WD_ALIGN_PARAGRAPHdoc = Document("0.docx")
for paragraph in doc.paragraphs:for run in paragraph.runs:run.font.bold = True #黑体run.font.italic = True #斜体run.font.underline = True #下划线run.font.strike = True #删除线run.font.shadow = True #阴影run.font.size = Pt(15) #字体大小run.font.color.rgb = RGBColor(255,255,0) #颜色run.font.name = "微软雅黑" #字体r = run._element.rPr.rFonts #中文字体r.set(qn('w:eastAsia'),"微软雅黑")paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER #duiqiparagraph.paragraph_format.line_spacing = 2.0 #用浮点数,表示两倍行间距paragraph.paragraph_format.space_before = Pt(12) #pt(21)表示21磅paragraph.paragraph_format.space_after = Pt(21)doc.save("xin1.docx")
结果:
后续将会持续更新excel,ppt,爬虫,人工智能等相关内容,敬请关注
【PYTHON,WORD】3.调整Word文档样式
1.修改文字样式
from docx import Document
from docx.shared import Pt,RGBColor
from docx.oxml.ns import qndoc = Document("0.docx")
for paragraph in doc.paragraphs:for run in paragraph.runs:run.font.bold = True #黑体run.font.italic = True #斜体run.font.underline = True #下划线run.font.strike = True #删除线run.font.shadow = True #阴影run.font.size = Pt(15) #字体大小run.font.color.rgb = RGBColor(255,255,0) #颜色run.font.name = "微软雅黑" #字体r = run._element.rPr.rFonts #中文字体r.set(qn('w:eastAsia'),"微软雅黑")doc.save("xin0.docx")
2.修改段落格式
2.1对齐样式:
from docx.enum.text import WD_ALIGN_PARAGRAPHparagraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
也可选择其他不同对齐
如:LEFT,RIGHT等
2.2行间距
paragraph.paragraph_foramt.line_spacing = 2.0
paragraph.paragraph_foramt.line_spacing = 2.0
用浮点数,表示两倍行间距
2.3段前和段后间距
paragraph.paragraph_format.space_before = Pt(12)
paragraph.paragraph_format.space_after = Pt(21)
pt(21)表示21磅
3.整体代码
from docx import Document
from docx.shared import Pt,RGBColor
from docx.oxml.ns import qn
from docx.enum.text import WD_ALIGN_PARAGRAPHdoc = Document("0.docx")
for paragraph in doc.paragraphs:for run in paragraph.runs:run.font.bold = True #黑体run.font.italic = True #斜体run.font.underline = True #下划线run.font.strike = True #删除线run.font.shadow = True #阴影run.font.size = Pt(15) #字体大小run.font.color.rgb = RGBColor(255,255,0) #颜色run.font.name = "微软雅黑" #字体r = run._element.rPr.rFonts #中文字体r.set(qn('w:eastAsia'),"微软雅黑")paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER #duiqiparagraph.paragraph_format.line_spacing = 2.0 #用浮点数,表示两倍行间距paragraph.paragraph_format.space_before = Pt(12) #pt(21)表示21磅paragraph.paragraph_format.space_after = Pt(21)doc.save("xin1.docx")
结果:
后续将会持续更新excel,ppt,爬虫,人工智能等相关内容,敬请关注