Python 提供了多种库来处理各种文档格式的样式,包括Word、PDF、Excel等。
以下是主要文档格式的样式处理方法:
文档的样式处理一般用于写入操作,
Python-docx
库里的样式主要有段落样式与字符样式,
并且对列表与表格两种对象的样式有独立的处理方法。
from docx import Document
from docx.shared import Cm
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.enum.style import WD_STYLE_TYPE
from docx.shared import Inches , Pt, RGBColor , Length
nfile = Document()
添加标题与段落:
nfile.add_heading("demo1",0)
nfile.add_paragraph('Lorem ipsum dolor sit amet.', style='List Bullet')
nfile.add_paragraph('Lorem ipsum dolor sit amet.', style='List Bullet')
nfile.add_paragraph('Lorem ipsum dolor sit amet.', style='List Bullet')
nfile.add_paragraph('Lorem ipsum dolor sit amet.', style='List Bullet')
nfile.add_paragraph('Lorem ipsum dolor sit amet.', style='List Bullet')
<docx.text.paragraph.Paragraph at 0x7f3280716e10>
这种特殊的样式使段落显示为项目符号,使用非常方便。也可以在以后应用样式。这两行等效于上面的那一行:
paragraph = nfile.add_paragraph('Lorem ipsum dolor sit amet.')
paragraph.style = 'List Bullet'
在此示例中,使用样式名称“ List Bullet”指定样式。通常,样式名称与在Word用户界面(UI)中显示的名称完全相同。
paragraphs=nfile.paragraphs
paragraphs[1].paragraph_format.first_line_indent = Inches(-0.25)
paragraphs[3].paragraph_format.left_indent=Cm(0.74)
paragraphs[5].paragraph_format.right_indent=Cm(2)
对齐方式:
LEFT
左对齐CENTER
文字居中RIGHT
右对齐JUSTIFY
文本两端对齐
与word软件内容的功能对应。对齐方式还有很多,以常用的为例:
paragraphs[1].paragraph_format.alignment=WD_ALIGN_PARAGRAPH.LEFT
paragraphs[3].paragraph_format.alignment=WD_ALIGN_PARAGRAPH.RIGHT
paragraphs[4].paragraph_format.alignment=WD_ALIGN_PARAGRAPH.CENTER
paragraphs[1].paragraph_format.alignment=WD_ALIGN_PARAGRAPH.JUSTIFY
paragraphs[1].paragraph_format.space_before = Pt(18)
paragraphs[3].paragraph_format.space_after = Pt(10)
paragraphs[5].paragraph_format.line_spacing = Pt(18)
paragraphs[3].paragraph_format.line_spacing = 1.5
paragraphs[3].paragraph_format.keep_with_next = True
paragraphs[5].paragraph_format.page_break_before = True
paragraph =nfile.add_paragraph('Lorem ipsum ')
paragraph.add_run('dolor sit amet.')
<docx.text.run.Run at 0x7f3280797890>
这将产生一个看起来像是从单个字符串创建的段落。 除非查看XML,否则不知道段落文本在何处拆分。 请注意第一个字符串末尾的尾随空格。 需要明确说明运行开始和结束处出现空格的位置。
Run对象具有 .bold
和 .italic
属性,可以设置运行的值:
paragraph = nfile.add_paragraph('Lorem ipsum ')
run = paragraph.add_run('dolor')
run.bold = True
paragraph.add_run(' sit amet.')
<docx.text.run.Run at 0x7f32805c6b40>
也可以这样写:
p = nfile.add_paragraph('A plain paragraph having some ')
p.add_run('bold').bold = True
p.add_run(' and some ')
p.add_run('italic.').italic = True
使用.add_run()
方法时可以在结果上设置粗体或斜体:
paragraph.add_run('dolor').bold = True
run = paragraph.add_run('dolor')
run.bold = True
paragraph = nfile.add_paragraph('Normal text,字符样式 ')
paragraph.add_run('text with emphasis.', 'Emphasis')
<docx.text.run.Run at 0x7f32805c6e10>
创建样式后,还可以将其应用于运行。这段代码产生与上面几行相同的结果:
paragraph = nfile.add_paragraph('Normal text, ')
run = paragraph.add_run('text with emphasis.')
run.style = 'Emphasis'
与段落样式一样,样式名称与在Word UI中显示的一样。
前面设置了字体的粗体和斜体,在官方 Font
类下还其他的相关设置。
设置字体属性
对 run
设置字体、大小、颜色下划线等,如下:
all_caps
全部大写字母bold
加粗color
字体颜色complex_script
是否为“复杂代码”cs_bold
“复杂代码”加粗cs_italic
“复杂代码”斜体double_strike
双删除线emboss
文本以凸出页面的方式出现hidden
隐藏imprint
印记italic
斜体name
字体no_proof
不验证语法错误outline
显示字符的轮廓shadow
阴影small_caps
小型大写字母snap_to_grid
定义文档网格时对齐网络strike
删除线subscript
下标superscript
上标underline
下划线
nfile.save('xx_style1.docx')
更加合理的,是对文档的命名样式进行修改,而不是逐个修改。
document=Document(r"xx_style1.docx")
styles = document.styles
type(styles)
docx.styles.styles.Styles
paragraph_styles = [s for s in styles if s.type == WD_STYLE_TYPE.PARAGRAPH]
for style in paragraph_styles:
print(style.name , end = '; ' )
Normal; Header; Footer; Heading 1; Heading 2; Heading 3; Heading 4; Heading 5; Heading 6; Heading 7; Heading 8; Heading 9; No Spacing; Title; Subtitle; List Paragraph; Body Text; Body Text 2; Body Text 3; List; List 2; List 3; List Bullet; List Bullet 2; List Bullet 3; List Number; List Number 2; List Number 3; List Continue; List Continue 2; List Continue 3; macro; Quote; Caption; Intense Quote; TOC Heading;
tt = styles['Normal']
tt.name
'Normal'
tt.paragraph_format.left_indent = Cm(8)
tt.font.bold = True
tt.font.color.rgb = RGBColor(0x00, 0xff, 0xff)
document.save('xx_style2.docx')