1. 样式
1.1 访问样式
使用Document.styles属性访问样式。
from docx import Document
document = Document()
# 获取样式对象,这里面可以像字典一样访问,也可以迭代
styles = document.styles
for style in styles:
print(style.name, "\t", style.type)
# 获取一个正文样式
print(styles['Normal'])
默认样式以英文方式存储,因此需要通过英文名称访问。
以下是默认样式的列表
Paragraph styles in default template
- Normal
- Body Text
- Body Text 2
- Body Text 3
- Caption
- Heading 1
- Heading 2
- Heading 3
- Heading 4
- Heading 5
- Heading 6
- Heading 7
- Heading 8
- Heading 9
- Intense Quote
- List
- List 2
- List 3
- List Bullet
- List Bullet 2
- List Bullet 3
- List Continue
- List Continue 2
- List Continue 3
- List Number
- List Number 2
- List Number 3
- List Paragraph
- Macro Text
- No Spacing
- Quote
- Subtitle
- TOCHeading
- Title
Character styles in default template
- Body Text Char
- Body Text 2 Char
- Body Text 3 Char
- Book Title
- Default Paragraph Font
- Emphasis
- Heading 1 Char
- Heading 2 Char
- Heading 3 Char
- Heading 4 Char
- Heading 5 Char
- Heading 6 Char
- Heading 7 Char
- Heading 8 Char
- Heading 9 Char
- Intense Emphasis
- Intense Quote Char
- Intense Reference
- Macro Text Char
- Quote Char
- Strong
- Subtitle Char
- Subtle Emphasis
- Subtle Reference
- Title Char
Table styles in default template
- Table Normal
- Colorful Grid
- Colorful Grid Accent 1
- Colorful Grid Accent 2
- Colorful Grid Accent 3
- Colorful Grid Accent 4
- Colorful Grid Accent 5
- Colorful Grid Accent 6
- Colorful List
- Colorful List Accent 1
- Colorful List Accent 2
- Colorful List Accent 3
- Colorful List Accent 4
- Colorful List Accent 5
- Colorful List Accent 6
- Colorful Shading
- Colorful Shading Accent 1
- Colorful Shading Accent 2
- Colorful Shading Accent 3
- Colorful Shading Accent 4
- Colorful Shading Accent 5
- Colorful Shading Accent 6
- Dark List
- Dark List Accent 1
- Dark List Accent 2
- Dark List Accent 3
- Dark List Accent 4
- Dark List Accent 5
- Dark List Accent 6
- Light Grid
- Light Grid Accent 1
- Light Grid Accent 2
- Light Grid Accent 3
- Light Grid Accent 4
- Light Grid Accent 5
- Light Grid Accent 6
- Light List
- Light List Accent 1
- Light List Accent 2
- Light List Accent 3
- Light List Accent 4
- Light List Accent 5
- Light List Accent 6
- Light Shading
- Light Shading Accent 1
- Light Shading Accent 2
- Light Shading Accent 3
- Light Shading Accent 4
- Light Shading Accent 5
- Light Shading Accent 6
- Medium Grid 1
- Medium Grid 1 Accent 1
- Medium Grid 1 Accent 2
- Medium Grid 1 Accent 3
- Medium Grid 1 Accent 4
- Medium Grid 1 Accent 5
- Medium Grid 1 Accent 6
- Medium Grid 2
- Medium Grid 2 Accent 1
- Medium Grid 2 Accent 2
- Medium Grid 2 Accent 3
- Medium Grid 2 Accent 4
- Medium Grid 2 Accent 5
- Medium Grid 2 Accent 6
- Medium Grid 3
- Medium Grid 3 Accent 1
- Medium Grid 3 Accent 2
- Medium Grid 3 Accent 3
- Medium Grid 3 Accent 4
- Medium Grid 3 Accent 5
- Medium Grid 3 Accent 6
- Medium List 1
- Medium List 1 Accent 1
- Medium List 1 Accent 2
- Medium List 1 Accent 3
- Medium List 1 Accent 4
- Medium List 1 Accent 5
- Medium List 1 Accent 6
- Medium List 2
- Medium List 2 Accent 1
- Medium List 2 Accent 2
- Medium List 2 Accent 3
- Medium List 2 Accent 4
- Medium List 2 Accent 5
- Medium List 2 Accent 6
- Medium Shading 1
- Medium Shading 1 Accent 1
- Medium Shading 1 Accent 2
- Medium Shading 1 Accent 3
- Medium Shading 1 Accent 4
- Medium Shading 1 Accent 5
- Medium Shading 1 Accent 6
- Medium Shading 2
- Medium Shading 2 Accent 1
- Medium Shading 2 Accent 2
- Medium Shading 2 Accent 3
- Medium Shading 2 Accent 4
- Medium Shading 2 Accent 5
- Medium Shading 2 Accent 6
- Table Grid
1.2 应用样式
Paragraph, Run, Table对象都有一个style属性,赋值即可应用样式。
from docx import Document
document = Document()
# 添加时指定样式
document.add_paragraph("标题1", style="Heading 1")
# 直接通用字符串指定样式
p = document.add_paragraph("标题2")
p.style = "Heading 2"
# 通过字典的方式指定样式
p = document.add_paragraph("标题3")
p.style = document.styles['Heading 3']
document.save("1.docx")
1.3 添加和删除样式
1.3.1 添加样式
from docx import Document
from docx.enum.style import WD_STYLE_TYPE
from docx.shared import Pt
document = Document()
styles = document.styles
# 添加新样式。名字是:My_style, type是:PARAGRAPH
# type: CHARACTER, LIST, PARAGRAPH, TABLE
style = styles.add_style('My_style', style_type=WD_STYLE_TYPE.PARAGRAPH)
style.font.size = Pt(16)
document.add_paragraph("hello world", style='My_style')
document.save("1.docx")
1.3.2 删除样式
styles['My_style'].delete()
1.4 字符与段落样式【例】
from docx import Document
from docx.enum.style import WD_STYLE_TYPE
from docx.oxml.ns import qn
from docx.shared import Pt
text = '燕子去了,有再来的时候;杨柳枯了,有再青的时候;桃花谢了,有再开的时候。但是,聪明的,你告诉我,我们的日子为什么一去不复返呢?——是有人偷了他们罢:那是谁?又藏在何处呢?是他们自己逃走了罢:如今又到了哪里呢?'
document = Document()
styles = document.styles
# 添加样式
style = styles.add_style('My_style', style_type=WD_STYLE_TYPE.PARAGRAPH)
style.font.size = Pt(16)
style.font.name = "仿宋"
style.font.element.rPr.rFonts.set(qn('w:eastAsia'), style.font.name)
# 首先缩进
style.paragraph_format.first_line_indent = style.font.size * 2
# 1.5倍行距
style.paragraph_format.line_spacing = 1.5
document.add_paragraph(text, style='My_style')
document.save("1.docx")
style里面的font可以设置字符样式,paragraph_format可以设置段落样式。
2. 表格
2.1 添加表格
from docx import Document
document = Document()
# 添加5行3列的表格
table = document.add_table(5, 3, style="Table Grid")
document.save("1.docx")
2.2 添加行和列
from docx import Document
from docx.shared import Cm
document = Document()
table = document.add_table(rows=1, cols=1, style="Table Grid")
# 下面加一行
table.add_row()
# 右侧加一列,宽度3.5厘米
table.add_column(Cm(3.5))
document.save("1.docx")
2.3 行和列对象
结构:一个表格(Table),表格里有行(Row),列(Column),行或列里有单元格(Cell)
from docx import Document
document = Document()
table = document.add_table(rows=5, cols=4, style="Table Grid")
# 获取所有行
print(table.rows)
# 获取所有列
print(table.columns)
# 按行遍历单元格
for row in table.rows:
for cell in row.cells:
print(cell)
# 按列遍历单元格
for col in table.columns:
for cell in col.cells:
print(cell)
document.save("1.docx")
在行和列对象里,有一个table属性,指向所属表格对象。
from docx import Document
document = Document()
table = document.add_table(rows=5, cols=4, style="Table Grid")
print(table.rows[1].table)
2.3.1 行高和列宽
from docx import Document
from docx.enum.table import WD_ROW_HEIGHT_RULE
from docx.shared import Cm
document = Document()
table = document.add_table(rows=5, cols=4, style="Table Grid")
for i in range(len(table.rows)):
if i == 2:
# 指定行高
table.rows[i].height = Cm(3)
else:
# 精确值
table.rows[i].height_rule = WD_ROW_HEIGHT_RULE.EXACTLY
table.rows[i].height = Cm(0.2)
document.save("1.docx")
WD_ROW_HEIGHT_RULE中可以指定:
-
AUTO 调整行高以适应行中的最高值。
-
AT_LEAST 行高至少是指定的最小值。
-
EXACTLY 行高是一个精确的值。
列宽我测试单独设置一个格不好使,所有单元格都设置才有用:
from docx.shared import Cm
document = Document()
table = document.add_table(rows=5, cols=4, style="Table Grid")
# 第3列
col = table.columns[2]
for c in col.cells:
c.width = Cm(15)
document.save("1.docx")
2.4 单元格
document = Document()
table = document.add_table(rows=5, cols=4, style="Table Grid")
# 指定位置
cell = table.cell(0,0)
print(cell)
2.4.1 单元格文本
document = Document()
table = document.add_table(rows=5, cols=4, style="Table Grid")
# 样式
table.style.font.size = Pt(15)
table.style.font.color.rgb = RGBColor(0, 255, 255)
cell = table.cell(0, 0)
cell.text = "hello world"
cell: _Cell = table.cell(0, 1)
cell.text = "第二格"
# 垂直对齐
cell.vertical_alignment = WD_CELL_VERTICAL_ALIGNMENT.CENTER
# 水平对齐
cell.paragraphs[0].paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
document.save("1.docx")
通过text读取和写入,也可以获取cell中的段落,之后添加文本并修饰。
标签:docx,Medium,python,Accent,List,style,Grid,document From: https://www.cnblogs.com/three-sheep/p/17418730.html