from docx import Document
#
document = Document(docx=None)
#
document.save('demo.docx')
from docx import Document
document = Document()
p = document.add_paragraph('A plain paragraph having some ')
p.add_run(' and some ')
p2 = p.insert_paragraph_before('Intense')
document.save('1.docx')
from docx import Document
document = Document()
#
document.add_heading('Document Title', 0)
document.add_heading('Heading, level 1', level=1)
document.close()
from docx import Document
from docx.shared import Inches
document = Document()
document.add_picture('monty-truth.png', width=Inches(1.25))
document.save('demo.docx')
from docx import Document
document = Document()
records = (
(3, '101', 'Spam'),
(7, '422', 'Eggs'),
(4, '631', 'Spam, spam, eggs, and spam')
)
# 添加表格
table = document.add_table(rows=1, cols=3)
# 添加行
table.add_row()
# 添加列
table.add_column()
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Qty'
hdr_cells[1].text = 'Id'
hdr_cells[2].text = 'Desc'
for qty, id, desc in records:
row_cells = table.add_row().cells
row_cells[0].text = str(qty)
row_cells[1].text = id
row_cells[2].text = desc
from docx import Document
document = Document()
document.add_page_break()
from docx import Document
document = Document()
p = document.add_paragraph('aaa')
p.add_run('bold').bold = True
p.add_run('italic.').italic = True
# style样式 'Intense Quote' 'List Bullet' 'List Number'
p = document.add_paragraph('Intense quote', style='Intense Quote')
from docx import Document
from docx.shared import Inches
from docx.shared import Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH
document = Document()
paragraph = document.add_paragraph()
paragraph_format = paragraph.paragraph_format
paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
# 首行缩进 负值表示挂起
paragraph_format.first_line_indent = Inches(-0.25)
print(paragraph_format.first_line_indent.inches)
# 左缩进
paragraph_format.left_indent = Inches(0.5)
print(paragraph_format.left_indent.inches)
# 右缩进
paragraph_format.right_indent = Pt(24)
print(paragraph_format.right_indent.pt)
document.save('demo.docx')
from docx import Document
from docx.shared import Pt
from docx.shared import Inches
from docx.enum.text import WD_TAB_ALIGNMENT, WD_TAB_LEADER
document = Document()
paragraph = document.add_paragraph()
paragraph_format = paragraph.paragraph_format
tab_stops = paragraph_format.tab_stops
# TIANJIA
tab_stop = tab_stops.add_tab_stop(Inches(1.5))
print(tab_stop.position.inches)
tab_stop = tab_stops.add_tab_stop(Inches(1.5), WD_TAB_ALIGNMENT.RIGHT, WD_TAB_LEADER.DOTS)
print(tab_stop.alignment)
print(tab_stop.leader)
from docx import Document
from docx.shared import Pt
from docx.shared import Inches
from docx.shared import RGBColor
from docx.enum.dml import MSO_THEME_COLOR
from docx.enum.text import WD_TAB_ALIGNMENT, WD_TAB_LEADER
document = Document()
run = document.add_paragraph().add_run()
font = run.font
font.name = 'Calibri'
font.size = Pt(12)
font.italic = True # False None
font.underline = True # False None WD_UNDERLINE枚举
# 颜色 rgb或枚举
print(font.color.type) # 查看字体颜色类型 font.color.rgb font.color.theme_color
font.color.rgb = RGBColor(0x42, 0x24, 0xE9)
font.color.theme_color = MSO_THEME_COLOR.ACCENT_1
from docx import Document
from docx.enum.section import WD_SECTION
document = Document()
# 获取所有节
sections = document.sections
# 获取最后一个
current_section = document.sections[-1]
# 添加节
new_section = document.add_section(WD_SECTION.ODD_PAGE)
from docx import Document
from docx.enum.section import WD_SECTION
document = Document()
section = document.sections[-1]
# 查看区间起始类型
print(section.start_type)
# CONTINUOUS 连续断开。
# NEW_COLUMN 新建列分节符。
# NEW_PAGE 新建分页符。
# EVEN_PAGE 偶数页分节符。
# ODD_PAGE 这一节从下一页开始
# 更改起始类型
section.start_type = WD_SECTION.NEW_PAGE
from docx import Document
from docx.enum.section import WD_SECTION
from docx.enum.section import WD_ORIENT
document = Document()
section = document.sections[-1]
print(section.orientation)
print(section.page_width)
print(section.page_height)
# 更改
new_width, new_height = section.page_height, section.page_width
section.orientation = WD_ORIENT.LANDSCAPE
section.page_width = new_width
section.page_height = new_height
from docx import Document
from docx.shared import Inches
from docx.enum.section import WD_SECTION
document = Document()
section = document.sections[-1]
section.left_margin
section.right_margin
section.top_margin
section.bottom_margin
section.gutter
section.header_distance
section.footer_distance
section.left_margin = Inches(1.5)
section.right_margin = Inches(1)
from docx import Document
document = Document()
section = document.sections[-1]
header = section.header
paragraph = header.paragraphs[0]
paragraph.text = "Title \t Date \t Content"
paragraph.style = document.styles["Header"]
# 表示是否继承上一节的页眉 默认继承
# 移除页眉
header.is_linked_to_previous = False
见 https://www.osgeo.cn/python-docx/api
from docx import Document
from docx.styles.styles import Styles
from docx.enum.style import WD_STYLE_TYPE
document = Document()
# 获取所有样式
styles = document.styles
print(styles)
# 取出样式库中的样式
style_normal = styles['Normal']
style_header1 = styles['Heading 1']
# 查看样式名称
print(style_normal.name)
# 查看样式类型
print(style_normal.type)
# 添加样式
style1 = styles.add_style('Citation', WD_STYLE_TYPE.PARAGRAPH)
style2 = [s for s in styles if s.type == WD_STYLE_TYPE.PARAGRAPH]
for style in style2:
print(style.name)
# 删除样式
styles['Citation'].delete()
# 从样式库中删除“普通”段落样式,但允许它保留在建议列表中
style = document.styles['Normal']
style.hidden = False
style.quick_style = False
# 指定插入的下一段落的默认样式
styles['Heading 1'].next_paragraph_style = styles['Body Text']
# 继承(创建新段落时默认样式)
style.base_style = styles['Normal']
# 样式的5个属性
# hidden
# unhide_when_used
# priority
# quick_style
# locked
# 获取潜在样式
latent_styles = document.styles.latent_styles
# 取出潜在样式
latent_style_names = [ls.name for ls in latent_styles] # 获取所有样式的name
print(latent_style_names)
latent_quote = latent_styles['Normal']
print(latent_quote.priority) # 查看样式的索引
latent_styles.default_to_locked = True
# 添加潜在样式
latent_style = latent_styles.add_latent_style('List Bullet')
latent_style.hidden = False
latent_style.priority = 2
latent_style.quick_style = True
# 删除潜在样式
latent_styles['Light Grid'].delete()
from docx import Document
document = Document()
# 获取所有样式
styles = document.styles
style_normal = styles['Normal']
## 创建时指定样式
paragraph = document.add_paragraph('Title', style='Body Text')
paragraph = document.add_paragraph('Title', style=style_normal)
## 创建后修改样式
paragraph.style = document.styles['Heading 1']
paragraph.style = 'List Bullet'
print(paragraph.style.name)
from docx import Document
from docx.shared import Pt
document = Document()
# 字符样式 docx.text.run.Font
style = document.styles['Normal']
font = style.font
font.name = 'Calibri'
font.size = Pt(12)
font.bold = None # None True False True表示打开 False关闭 None继承上一等级的配置
font.italic = None # None True False
# 下划线:https://www.osgeo.cn/python-docx/api/enum/WdUnderline.html#wdunderline
# True 表示单下划线,False 没有下划线, None 继承, WD_UNDERLINE.WORDS 枚举其它形式
font.underline = None
布局行为,如对正、缩进、前后空格、前后分页符和窗口/孤立控件
from docx import Document
from docx.enum.style import WD_STYLE_TYPE
from docx.shared import Inches, Pt
document = Document()
style = document.styles.add_style('Indent', WD_STYLE_TYPE.PARAGRAPH)
paragraph_format = style.paragraph_format
# 左缩进
paragraph_format.left_indent = Inches(0.25)
# 首行缩进
paragraph_format.first_line_indent = Inches(-0.25)
# 上面有12个点的间距
paragraph_format.space_before = Pt(12)
#
paragraph_format.widow_control = True
见 docx.opc.coreprops.CoreProperties
所有属性类型均为str、datetime.datetime 或 int
之一
见docx.styles.styles.Styles
from docx import Document
doc = Document()
# 获取所有样式库 返回可迭代对象 支持键值对访问
styles = doc.styles
# 查看样式个数
print(len(styles))
# 添加样式 name 样式名称 样式类型 是否为内置样式
styles.add_style(name='', style_type='', builtin=False)
# 返回的默认样式 style_type
styles.default(style_type='')
# 取出样式 docx.styles.style.BaseStyle
style = styles['Normal']
# 删除样式
style.delete()
样式
见 docx.text.paragraph.Paragraph
from docx import Document
document = Document()
# 添加段落
p = document.add_paragraph(text='aa', style=None)
# 段落后追加文字
p.add_run(text='bb', style=None)
# 删除所有内容后返回同一段落。段落级格式(如样式)将保留
p.clear()
# 创建段落
p.insert_paragraph_before(text=None, style=None)
# 段落属性
p.alignment
p.paragraph_format
p.runs
p.style
p.text
from docx import Document
document = Document()
# 添加段落
p = document.add_paragraph(text='aa', style=None)
# 对齐方式
p.alignment = None
# LEFT 左对齐
# CENTER居中对齐。
# RIGHT右对齐。
# JUSTIFY完全合理。
# DISTRIBUTE段落字符被分布以填充段落的整个宽度。
# JUSTIFY_MED以中等字符压缩比调整。
# JUSTIFY_HI具有高字符压缩比。
# JUSTIFY_LOW以较低的字符压缩比进行调整。
# THAI_JUSTIFY根据泰语格式布局对齐。
p.first_line_indent = None
# True 在呈现文档时,段落是否应保持“完整”且不应跨越页面边界。 None 指示其有效值是从样式层次结构继承的
p.keep_together = None
# True 当提交文件时,该段是否应与下一段保持在同一页上。例如,此属性可用于将节标题与其第一段保持在同一页上。 None 指示其有效值是从样式层次结构继承的
p.keep_with_next
# Length 值,指定段落的左边距和左边距之间的间距。 None 指示左缩进值是从样式层次结构继承的。使用 Inches 值对象作为以英寸为单位应用缩进的方便方法。
p.left_indent
# float 或 Length 指定段落连续行中基线之间的间距的值。价值 None 指示行距是从样式层次结构继承的。浮点值,例如。 2.0 或 1.75 ,表示间距以行高的倍数应用。A Length 价值,例如 Pt(12) 表示间距是固定高度。这个 Pt 值类是以点为单位应用行距的便捷方法。分配 None 重置行距以继承样式层次结构。
p.line_spacing
p.line_spacing_rule
p.page_break_before
# Length 值,指定段落的右边距和右边距之间的间距。 None 指示右缩进值是从样式层次结构继承的。使用 Cm 值对象作为以厘米为单位应用缩进的方便方法。
p.right_indent
# Length 值,指定此段落与后续段落之间的间距。 None 指示此值是从样式层次结构继承的。 Length 对象提供方便的属性,例如 pt 和 inches ,可轻松转换为各种长度单位。
p.space_after
p.space_before
p.tab_stops
p.widow_control
docx.shared.RGBColor(r, g, b) 例 lavender = RGBColor(0xff, 0x99, 0xcc)
枚举模块