导图社区 Word库知识总结
本导图梳理了python-docx官方文档的知识内容,重点方法都有代码,python办公自动化操纵word,office必用!
编辑于2021-08-27 23:39:38python-word
Document docs = Document()
add_heading(text=u, level=1-9)
document.add_heading('Document Title', 0)
document.add_heading('Document Title', 0)
add_page_break()
Return newly Paragraph object containing only a page break.
add_paragraph(text=u, style=None)
docs.add_heading('Document Title', 0)
add_picture(path, width=None, height=None)
add_section(start_type=2)
增加新节,默认含有一个节
sec1 = docs.sections[0]
print(len(docs.sections)) # 1
sec2 = docs.add_section()
print(len(docs.sections)) # 2
add_table(rows, cols, style=None)
table = docs.add_table(rows=1, cols=3)
save(path_or_stream)
inline_shapes
An inline shape is a graphical object, such as a picture
height
width
print(inline_shape.height) # 914400
print(inline_shape.height.inches) # 1.0
type
styles
providing access to the styles in this document
add_style(name, style_type, builtin=False)
default(style_type)
element
latent_styles
settings
providing access to each section in this document.
tables
providing access to the styles in this document corresponding to the tables in the document, in document order
path = '123.docx' #文件路径
document = Document(path) #读入文件
tables = document.tables #获取文件中的表格集
for table in tables[:]: #打印所有表格
part
sections
providing access to each section in this document
core_properties
文档属性
author
文档的作者
print(docs.core_properties.author) # python-docx
category
Resume, Letter, Financial Forecast, Proposal, or Technical Presentation.
comments
print(docs.core_properties.comments) # generated by python-docx
content_status
created
identifier
keywords
language
文档使用的语言
last_modified_by
last_printed
modified
revision
subject
title
version
section
bottom_margin
设置这一节的底边距
docs = Document()
sec1 = docs.sections[0]
p1 = docs.add_paragraph('This is 1st para. ')
sec1.bottom_margin = Inches(3)
sec1.right_margin = Cm(3)
sec1.top_margin = Inches(1)
from docx.shared import Inches, Pt, Cm, Mm, RGBColor, Twips, Emu
different_first_page_header_footer
如果设置本节显示不同于第一页页眉和页脚,则为True。
sec1.different_first_page_header_footer=True
even_page_footer
设置偶数页页脚
even_page_header
设置偶数页页眉
first_page_footer
设置第一页页脚
first_page_header
设置第一页页眉
footer
页脚设置
footer1 = sec1.footer
footer1.paragraphs[0].text="第一节页脚"
header1 = sec1.header
header1.paragraphs[0].text="第一节页眉"
footer_distance
设置页脚上边距
The DocumentPart object of this document.
footer1.paragraphs[0].footer_distance = Cm(5)
gutter
装订线是到内部边距的间距 页面到左边的距离
sec1.gutter = Cm(10)
header
设置页眉
header = docs.sections[0].header # 获取第一个节的页眉
paragraph = header.paragraphs[0] # 获取页眉的第一个段落
paragraph.add_run('这是第一节的页眉') # 添加页面内容
header_distance
页面顶部边缘到页眉顶部边缘的距离
left_margin
设置这一节的左边距
orientation
PORTRAIT:纵向
LANDSCAPE:横向
from docx.enum.section import WD_ORIENT
section = document.sections[-1]
section.orientation = WD_ORIENT.LANDSCAPE
page_height
设置页面高度
page_width
设置页面宽度
sec1.page_height = Cm(15)
sec1.page_width = Inches(15)
right_margin
设置这一节的右边距
start_type
指定分节符的开始类型
CONTINUOUS:连续
NEW_COLUMN:分栏符
NEW_PAGE:下一页
from docx.enum.section import WD_SECTION
section = document.sections[0]
section.start_type = WD_SECTION.NEW_PAGE
EVEN_PAGE:偶数页
ODD_PAGE:奇数页
top_margin
设置这一节的上边距
table object
table
add_column(width)
增加一列 可设置宽度
table = docs.add_table(rows=1, cols=3)
table.add_column(Cm(3))
add_row()
增加一行
table = docs.add_table(rows=1, cols=3)
table.add_column(Cm(3))
table.add_row()
alignment
表格内文字对齐方式
LEFT
CENTER
from docx.enum.table import WD_TABLE_ALIGNMENT
table = document.add_table(3, 3)
table.alignment = WD_TABLE_ALIGNMENT.CENTER
RIGHT
autofit
True:自动调整列宽以改善单元格内容的适应性
cell(row_idx, col_idx)
返回该位置的实例 (0, 0) is the top, left-most cell
column_cells(column_idx)
表中column_idx处的单元格的序列
columns
填充列的内容
table2 = docs.add_table(rows=1, cols=3)
table2.add_row()
c1_cells = table2.columns[0].cells
c1_cells[0].text = '1-1'
row_cells(row_idx)
表中row_idx处的单元格的序列。
rows
填充行的内容
table = docs.add_table(rows=1, cols=3)
table.add_row()
r1_cells = table.rows[0].cells
r1_cells[0].text = '11'
r1_cells[2].text = '13'
r2_cells = table.rows[1].cells
r2_cells[2].text = '23'
style
表格样式
Normal Table
Table Grid
table2 = docs.add_table(rows=1, cols=3,style='Table Grid')
Light Shading
Light Shading Accent 1~Light Shading Accent 6
Light List
Light List Accent 1~Light List Accent 6
Light Grid
Light Grid Accent 1~Light Grid Accent 6
https://blog.csdn.net/xtfge0915/article/details/83480120
table_direction
设置表格的方向 LTR 从左向右 RTL 从右向左
from docx.enum.table import WD_TABLE_DIRECTION
table = document.add_table(3, 3)
table.direction = WD_TABLE_DIRECTION.RTL
_cell
add_paragraph(text=u'', style=None)
表格单元格中插入段落 也可插入run,设置font
table.cell(2,2).add_paragraph('Is is useful to get python?')
add_table(rows, cols)
表格单元格中插入新表格
table.cell(2,1).add_table(rows=2,cols=3)
merge(other_cell)
合并单元格
table.cell(2,1).merge(table.cell(2,0))
paragraphs
List of paragraphs in the cell. 表格单元格中段落的列表
tables
List of tables in the cell, in the order they appear. 表格单元格中含有的表格的列表
text
填充单元格内容
table.cell(0,0).text = 'hello'
table.cell(1,1).text = 'python'
vertical_alignment
表格中文字的对齐方式
TOP
CENTER
BOTTOM
from docx.enum.table import WD_ALIGN_VERTICAL
table = document.add_table(3, 3)
table.cell(0, 0).vertical_alignment = WD_ALIGN_VERTICAL.BOTTOM
BOTH
width
设置表格单元格宽度
from docx.enum.table import WD_ALIGN_VERTICAL
table = docs.add_table(rows=3, cols=3,style='Table Grid') for i in range(len(table.rows)): table.rows[i].height = Cm(2) for j in range(len(table.columns)): table.cell(i,j).width = Cm(4) table.cell(i,j).vertical_alignment = WD_ALIGN_VERTICAL.CENTER
_row
cells
属于这一行的单元格
height
返回这一行的高度
height_rule
返回该单元格的高度规则,作为WD_ROW_HEIGHT_RULE 枚举的成员,如果没有设置明确的height_rule,则返回无。 AUTO AT_LEAST EXACTLY
from docx.enum.table import WD_ROW_HEIGHT_RULE
table = document.add_table(3, 3)
table.rows[0].height_rule = WD_ROW_HEIGHT_RULE.EXACTLY
table
Reference to the Table object this row belongs to.
_column
cells
属于这一列的单元格
table
返回属于这一列的表格
width
返回这一列的高度
_rows
table
Reference to the Table object this row collection belongs to.
_columns
table
Reference to the Table object this column collection belongs to.
paragraphs
p = document.add_paragraph('a paragraph')
add_run(text=None, style=None)
insert_paragraph_before(text=None, style=None)[source]
runs
Sequence of Run instances
style
text
alignment
clear()
Return this same paragraph after removing all its content
p2 = docs.add_paragraph('This is 2st para.')
p2.clear()
run
paragraph_format
alignment
设置段落对其方式
LEFT 左对齐
CENTER 文字居中
from docx.enum.text import WD_ALIGN_PARAGRAPH
p6=docs.add_paragraph('6 para')
p6.paragraph_format.alignment=WD_ALIGN_PARAGRAPH.CENTER
RIGHT 右对齐
JUSTIFY 本两端对齐
DISTRIBUTE
Paragraph characters are distributed to fill the entire width of the paragraph.
THAI_JUSTIFY
JUSTIFY_MED JUSTIFY_HI JUSTIFY_LOW
first_line_indent
首行缩进 默认Inches(0.5)等于四个空格
p7=docs.add_paragraph('PAPR 7')
p7.paragraph_format.first_line_indent=Inches(0.5)
Inches(0.25)是两个空格
keep_together
True时段落不会被分在两页
p9.paragraph_format.keep_together=True
keep_with_next
True与后面一段在同一页面上 可以用来将一个章节的标题与它的第一段保持在同一页面上
p8.paragraph_format.keep_with_next=True
p9=docs.add_paragraph('p9: Length value')
p8和p9会保持在同一页上
left_indent
左边悬挂缩进 整个段落缩进
p7=docs.add_paragraph('PAPR 7')
p7.paragraph_format.left_indent=Inches(0.5)
p7.paragraph_format.first_line_indent=Inches(0.25)
同时设置悬挂缩进和首行缩进
line_spacing
float:如1.5,2.0代表1.5和2倍行距 Pt:Pt(20)代表20磅行距
p8.paragraph_format.line_spacing=2.0
p9.paragraph_format.line_spacing=Pt(20)
line_spacing_rule
内置的一些行距
ONE_POINT_FIVE
1.5倍行距
AT_LEAST
最小值
DOUBLE
双倍行距
EXACTLY 固定值
p10 = docs.add_paragraph('p10 hahh')
p10.paragraph_format.line_spacing_rule = WD_LINE_SPACING.EXACTLY
MULTIPLE
多倍行距
SINGLE
单倍行距
page_break_before
在段落前面加分页符
p10 = docs.add_paragraph('p10 hahh')
p10.paragraph_format.page_break_before = True
right_indent
右边悬挂缩进
p11 = docs.add_paragraph('p11 arriving....')
p11.paragraph_format.right_indent = Inches(1)
space_after
设置这一段和后一段的之间 的距离,用Pt(30)或Inches(0.5)
p10.paragraph_format.space_after = Pt(60)
p11 = docs.add_paragraph('p11 arriving....')
p11.paragraph_format.right_indent = Inches(1)
space_before
设置这一段和后一段的之间 的距离,用Pt(30)或Inches(0.5)
p10.paragraph_format.space_before = Pt(60)
tab_stops
制表位决定了段落文字中制表符的呈现方式 指定了制表符后面的文本的开始位置, 与该位置对齐的方式
from docx.enum.text import WD_TAB_ALIGNMENT
p12 = docs.add_paragraph('p12 True erited from the style hierarchy.')
p12.paragraph_format.tab_stops.add_tab_stop(Inches(6), WD_TAB_ALIGNMENT.RIGHT)
widow_control
TRUE段落第一行和最后一行 都在同一页面上
TabStop
alignment
指定要应用的制表位对齐方式 WD_TAB_ALIGNMENT
LEFT
CENTER
RIGHT
DECIMAL
BAR
LIST
CLEAR EDN NUM START
leader
前导字符默认为空格, 可通过提供WD_TAB_LEADER设置
SPACES 默认
DOTS
DASHES
LINES
HEAVY
黑实线
MIDDLE_DOT
position
制表符的位置Inches() 必须是正数
TabStops
add_tab_stop(position, alignment=WD_TAB_ALIGNMENT.LEFT, leader=WD_TAB_LEADER.SPACES)
p12 = docs.add_paragraph('p12 True erited from the style hierarchy.')
tab_stops = p12.paragraph_format.tab_stops
tab_stops.add_tab_stop(Inches(5), WD_TAB_ALIGNMENT.CENTER,WD_TAB_LEADER.HEAVY)
在该段落中间任意位置按tab键, 制表符都会变为黑实线长度为5Inche
clear_all()
清除所有制表符
run
add_break(break_type=6)
添加制表符
add_paragraph(text=u, style=None)
add_tab()
增加一个制表符
run1 = para1.add_run()
run1.add_tab()
add_text(text)
run1.add_text('This is belong to run1.')
bold
p.add_run('test').bold = True
r3 = p1.add_run('This is belong to r3.')
r3.bold = True
clear()
删除run
run1.clear()
italic
p.add_run('test').italic = True
r4 = p1.add_run('This is belong to r4.')
r4.italic = True
style
run的样式
text
获取文本内容
underline
添加下划线
r2 = p1.add_run('This is belong to r1.')
r2.underline = True
font
font
all_caps
设置字母大写
r7= p4.add_run()
r7.add_text('This is r7.')
r7.font.all_caps = True
bold
设置字体粗体,布尔值
p4 = docs.add_paragraph('4 para')
r6= p4.add_run()
r6.add_text('This is r6.')
r6.font.bold = True
color
rgb
设置字体rgb颜色
r7= p4.add_run()
r7.add_text('This is r7.')
r7.font.color.rgb = RGBColor(255, 0, 0)
RGBColor(r, g, b)
theme_color
根据默认模板颜色设置字体颜色
BACKGROUND_1~BACKGROUND_2
DARK_1~DARK_2
LIGHT_1~LIGHT_2
TEXT_1~TEXT_2
ACCENT_1~ACCENT_6
from docx.enum.dml import MSO_THEME_COLOR
run1.font.color.theme_color = MSO_THEME_COLOR.ACCENT_1
MIXED
HYPERLINK
FOLLOWED_HYPERLINK
NOT_THEME_COLOR
type
Read-only
complex_script
复杂脚本:语言含有的字符串形状和布局,如阿拉伯语
r8.add_text('لغة عربية r8')
r8.font.complex_script = True
cs_bold
复杂脚本加粗,布尔值
r8.font.complex_script = True
r8.font.cs_bold = True
cs_italic
复杂脚本斜体,布尔值
r8.font.complex_script = True
r8.font.cs_italic = True
double_strike
双删除线,布尔值
r9 = p4.add_run()
r9.add_text('This is r9.')
r9.font.double_strike = True
emboss
压印浮凸字体,布尔值
r10 = p4.add_run()
r10.add_text('This is r10.')
r10.font.emboss = True
hidden
隐藏显示,布尔值
r10 = p4.add_run()
r10.add_text('This is r10.')
r10.font.hidden = True
highlight_color
设置高亮,默认黑色 A member of WD_COLOR_INDEX
AUTO BLACK BLUE RED PINK YELLOW VIOLET GREEN TEAL GRAY_25 GRAY_50 BRIGHT_GREEN DARK_BLUE DARK_RED DARK_YELLOW
from docx.enum.text import WD_COLOR_INDEX
r10.font.highlight_color = WD_COLOR_INDEX.YELLOW
imprint
文本压入纸面,布尔值
r10.font.imprint = True
italic
给字体设置斜体,布尔值
r7= p4.add_run()
r7.add_text('This is r7.')
r7.font.italic = True
math
设为数学公式
p5 = docs.add_paragraph('5 para')
r11 = p5.add_run()
r11.add_text('2+3=5.')
r11.font.math = True
name
设置字体名称
r1 = p1.add_run()
r1.add_tab()
r1.add_text('This is belong to r1.')
r1.font.name = 'Times New Roman'
no_proof
为True不进行拼写/语法检查
outline
设置空心字
r11.font.outline = True
rtl
right-to-left 从右到左倒着写
r11.add_text('2+3=5')
r11.font.rtl = True
size
设置字体大小
r11.add_text('50号字体')
r11.font.size = Pt(50)
print(r11.font.size.pt) # 50.0
shadow
添加阴影
r11.add_text('阴影字体')
r11.font.shadow = True
small_caps
字母大写但是比run小2号
r6= p4.add_run()
r6.add_text('This is r6.')
r6.font.small_caps = True
snap_to_grid
当为 "真 "时,导致运行在此运行中 铺设字符时使用docGrid元素中定义 的每一行的文档网格字符设置。
spec_vanish
在当前文档中显示隐藏文本
r11.add_text('隐藏文本')
r11.font.spec_vanish = True
strike
添加单删除线
r11.add_text('文本')
r11.font.strike = True
subscript
设置字体下标,布尔值
r5.font.subscript = True
superscript
设置字体上标,布尔值
r5.font.superscript = True
underline
添加下划线,布尔值
r2 = p1.add_run('This is belong to r2.')
r2.underline = True
web_hidden
当为 "真 "时,指定当文件在网页视图中 显示时,该运行的内容应被隐藏。
善于利用dir(方法)