0
点赞
收藏
分享

微信扫一扫

python操作word


python操作word_页眉


 ​​python-docx官方文档地址​​

安装  pip3 install python-docx    

         pip3 install python-docx -i https://pypi.douban.com/simple              豆瓣镜像下载


内联对象一般包括:段落(paragraph)、图片(inline picture)、表(table)、标题(heading)、有序列表(numbered lists)、无序列表(bullets lists)  



创建文档 

from docx import Document
from docx.shared import Inches
document = Document() #创建基于默认“模板”的空白文档



打开文档

document = Document('d:/test.docx')  #打开文档



添加段落 

paragraph = document.add_paragraph('段落1')  #在尾部添加段落
#参数 段落文本


在段落尾部添加文本

kuai=paragraph.add_run('我是中国人')  #在段落尾部添加文本
#返回值:内联对象
paragraphs=document.paragraphs   #返回段落引用集合--列表
paragraphs[1].text="小Z同学:" #设置序号1段落的文本





返回段落集合

s=document.paragraphs  #返回段落引用集合--列表



返回段落总数

s=len(document.paragraphs)  #返回段落总数


返回指定段落的文本

s=document.paragraphs[0].text  #返回指定段落的文本



设置段落样式

paragraph.style = 'List Bullet'  #设置段落样式
paragraph =document.add_paragraph('段落4',style = 'List Bullet') #添加段落--带段落样式



返回段落样式

s=document.paragraphs  #返回段落引用集合--列表
s1=s[0].style #返回序号0段落的样式
print(s1)




段落对齐

需要     from docx.enum.text import WD_ALIGN_PARAGRAPH

paragraph_format = paragraph.paragraph_format  #创建段落格式对象
paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER #段落居中对齐
paragraph_format.alignment =WD_ALIGN_PARAGRAPH.LEFT #段落左对齐
paragraph_format.alignment =WD_ALIGN_PARAGRAPH.RIGHT #段落右对齐
paragraph_format.alignment =WD_ALIGN_PARAGRAPH.JUSTIFY #段落两端对齐
paragraphs=document.paragraphs   #返回段落引用集合--列表
paragraphs[4].paragraph_format.alignment=WD_ALIGN_PARAGRAPH.RIGHT #序号4段落右对齐




段落缩进

段落可以在左侧和右侧分别缩进。第一行也可以具有与段落其余部分不同的缩进,缩进的第一行有一个​悬挂缩进

paragraph_format = paragraph.paragraph_format  #创建段落格式对象
paragraph_format.left_indent = Inches(0.5) #段落左缩进0.5英寸
#需要 from docx.shared import Inches
paragraph_format.right_indent = Pt(20) #右缩进20点
#from docx.shared import Pt
paragraph_format.first_line_indent = Inches(0.5) #第一行缩进


paragraphs=document.paragraphs   #返回段落引用集合--列表
paragraphs[2].paragraph_format.first_line_indent=Cm(0.74) #序号2段落首行缩进0.74厘米
#from docx.shared import Cm


段落间距

paragraph_format = paragraph.paragraph_format  #创建段落格式对象
paragraph_format.space_before = Pt(38) #设置段落前间距
paragraph_format.space_after = Pt(19) #设置段落后间距


行间距

paragraph_format = paragraph.paragraph_format  #创建段落格式对象
paragraph_format.line_spacing = Pt(50) #设置行间距


行距可以通过段落paragraph_format属性的line_spacing或line_spacing_rule属性来指定,当line_spacing设置为长度值时表示绝对距离,设置为浮点数时表示行高的倍数,设置为None表示根据继承层次决定 


保存文档

document.save('d:/test.docx')  #保存文档--覆盖原文档



添加标题

document.add_heading('标题', level=0)  #添加标题
#参数2 标题级别 0--9



添加分页

document.add_page_break()  #添加分页



换页方式

换页方式决定一个段落在一个页面结束附近如何表现,常用属性有如下,每个属性的取值可以为True、False、None:


  • keep_together设置为True时使得整个段落出现在同一页中,如果一个段落在换页时可能会被打断就在段前换页;
  • keep_with_next设置为True时使得本段与下一段出现在同一页中;
  • page_break_before设置为True时使得本段出现在新的一页的顶端,例如新的一章标题必须从新的一页开始;
  • window_control设置为True时表示可以在必要的时候进行分页,避免本段的第一行或最后一行单独出现在一页中


粗体和斜体

kuai=paragraph.add_run('我是中国人')  #在段落尾部添加文本
#返回值:内联对象
kuai.bold = True #给内联设置粗体
kuai=paragraph.add_run('我是中国人')  #在段落尾部添加文本
#返回值:内联对象
kuai.italic = True #给内联设置斜体
kuai.underline = True  #给内联设置下划线



字符格式

Run属于行内元素的一种,是一个块级元素的组成部分,可以看做是一段连续的具有相同格式(字体、字号、颜色、加粗、斜体、下画线、阴影等)的文本。一般来说,一个段落会包含一个或多个Run,使得同一个段落中可以包含不同格式的文本

可以通过一个Run对象的font属性来获取和设置该Run的字符格式,例如字体名称font.name、字体大小font.size、是否加粗font.bold、是否斜体font.italic、下画线格式font.underline(True表示单下画线,False表示没有下画线,或者使用WD_UNDERLINE中的成员设置更多下画线格式)、字体颜色font.color.rgb(设置为docx.shared.RGBColor对象)

包括字体字体和大小,粗体,斜体和下划线

#设置字体--麻烦一点
kuai.font.name=u'华文彩云'
r = kuai._element
r.rPr.rFonts.set(qn('w:eastAsia'), '华文彩云')
#需要 from docx.oxml.ns import qn

kuai.font.size = Pt(30) #字体大小
kuai.font.color.rgb = RGBColor(0x42, 0x24, 0xE9) #设置字体颜色
#需要 from docx.shared import RGBColor



样式

s=document.styles  #获取word所有样式集合对象
for i in s:
print(i)






章节 


from docx import Document
from docx.shared import Inches
from docx.enum.section import WD_ORIENT, WD_SECTION

document = Document()
paragraph = document.add_paragraph('段落1')
paragraph = document.add_paragraph('段落5:床前明月光,疑是地上霜。举头望明月,低头思故乡。')

document.add_section() #添加新章节
paragraph = document.add_paragraph('章节2-1')
document.add_section() #添加新章节
paragraph = document.add_paragraph('章节3-1')

sections = document.sections #返回所有章节引用的对象
s=len(sections) #返回章节总数
section = sections[0] #返回指定章节的对象
section = document.sections[-1] # 返回文档最后一个章节
new_height= section.page_height #返回章节页面的高
#10058400 单位:像素 1英寸=914400像素
new_width=section.page_width #返回章节页面的宽
#7772400
section.page_height=10058400 #设置章节的高度
section.page_width =4072400 #设置章节宽度

section.orientation = WD_ORIENT.LANDSCAPE #设置页面方向 ???
#需要 from docx.enum.section import WD_ORIENT, WD_SECTION

s=section.left_margin #返回左边距--单位像素
s=section.right_margin #返回右边距--单位像素
s=section.top_margin #返回上边距--单位像素
s=section.bottom_margin #返回下边距--单位像素
section.left_margin = Inches(1.5) #设置左边距
s=section.header_distance #返回页眉距离--单位像素
s=section.footer_distance #返回页脚距离--单位像素

print(s)
document.save('d:/test.docx')



页眉和页脚

Word支持页眉和页脚。页眉是出现在每个页面的上边距区域中的文本,与文本主体分开,并且通常传达上下文信息,例如文档标题,作者,创建日期或页码。文档中的页眉在页面之间是相同的,内容上只有很小的差异,例如更改部分标题或页码。页眉也称为运行头

页脚在页眉的每个方面都类似,只不过它出现在页面底部。它不应与脚注混淆,脚注在页面之间内容是不一致的

页眉和页脚与一个章节相关联,这允许每个章节具有不同的页眉和/或页脚

页眉:

每个section对象都有一个.header属性,可以访问该节的​​_Header​​对象:

from docx import Document

document = Document()
paragraph = document.add_paragraph('段落1')
document.add_section() #添加新章节
paragraph = document.add_paragraph('段落2')
document.add_section()
paragraph = document.add_paragraph('段落3')

section = document.sections[0] #返回序号0章节的引用
header = section.header #返回章节header引用
print(header.is_linked_to_previous) #章节头(页眉)是否无定义
#值为True表示_Header对象不包含章节头定义,该章节将显示与上一节相同的章节头

#添加页眉
paragraph = header.paragraphs[0] #返回页眉序号0的段落的引用
#章节头已包含单个(空)段落
#此时把header.is_linked_to_previous属性设为false
paragraph.text = "这是页眉1"

print(header.is_linked_to_previous)
document.save('d:/test.docx')




删除页眉

通过将True分配给其​​.is_linked_to_previous​​属性,可以删除不需要的页眉:

header = section.header  #返回章节header引用
header.is_linked_to_previous = True #删除页眉



页脚:

每个section对象都有一个footer属性,可以访问该章节的页脚对象:


section = document.sections[0]  #返回序号0章节的引用
footer=section.footer #返回章节页脚的引用
print(footer.is_linked_to_previous)
#值为True表示页脚对象不包含定义,该章节将显示与上一节相同的页脚

#添加页脚
paragraph = footer.paragraphs[0] #返回页脚序号0的段落的引用
#页脚已包含单个(空)段落
#此时把footer.is_linked_to_previous属性设为false
paragraph.text = "这是页脚1"



footer.is_linked_to_previous = True  #删除页脚


制表符 


tab_stops = paragraph.paragraph_format.tab_stops  #返回段落格式制表符的引用
from docx.enum.text import WD_TAB_ALIGNMENT, WD_TAB_LEADER
tab_stop = tab_stops.add_tab_stop(Inches(4.5), WD_TAB_ALIGNMENT.LEFT, WD_TAB_LEADER.DOTS) #添加制表位
#参数2 对齐--默认左 https://python-docx.readthedocs.io/en/latest/api/enum/WdTabAlignment.html#wdtabalignment
#参数3 填充符--默认空格 https://python-docx.readthedocs.io/en/latest/api/enum/WdTabLeader.html#wdtableader
print(tab_stop.position) #返回制表位位置--单位像素
print(tab_stop.position.inches) #返回制表位位置--单位英寸
tab_stops[0] #返回序号0制表位的引用





表格

添加表格 

table = document.add_table(rows=2, cols=2)  #添加表格



给单元格赋值和读取单元格文本 

cell = table.cell(0, 1)  #返回表格的单元格对象
cell.text = '0行1列' #给单元格赋值
s=cell.text #返回单元格文本
row = table.rows[1]  #返回行对象
row.cells[0].text = '一行零列' #给行对象的第n个单元格赋值
s=row.cells[1].text #返回行对象的第n个单元格的文本
col = table.columns[1]  #返回列对象
col.cells[0].text = '零行1列' #给列对象的第n个单元格赋值
s=col.cells[1].text #返回列对象的第n个单元格的文本
tables=document.tables  #返回文档所有表格引用集合--列表
tables[0].cell(1,0).text="猫粮1" #给序号0的表格指定单元格设置文本





总行数和总列数

s = len(table.rows)     #返回表格的总行数
s = len(table.columns) #返回表格的总列数




图片

添加图片 


document.add_picture('大象.png')  #添加图片--添加的图像以原始大小显示
document.add_picture('大象.png', width=Inches(1.0))  #添加图片
#参数2 图品宽度 -宽度和高度只指定一个,另一个按比例缩放
#Inches--单位是英寸
document.add_picture('大象.png', width=Cm(11.8))#添加图片
#需要   from docx.shared import Cm


举报

相关推荐

0 条评论