1. 页面布局

import docx
from docx import Document 
document = docx.Document()
    
    
##页面布局为横向
sections=document.sections
section=sections[0]
new_pagewidth,new_pageheight=section.page_height,section.page_width
#设置三个参数
section.orientation = WD_ORIENT.LANDSCAPE
section.page_height=new_pageheight
section.page_width=new_pagewidth

如果不设置pageheight 和pagewidth 那么转向就没有用啦
    

2. 页边距调整

    #changing the page margins修改页边距
    sections = document.sections 
    for section in sections: 
        section.top_margin = Cm(3) 
        section.bottom_margin = Cm(2) 
        section.left_margin = Cm(2.54) 
        section.right_margin = Cm(2.54) ##Cm 需要import 一下

3. 插入页眉(文字加表格)

 ##插入页眉
##页眉离页面顶端距离
##页脚离页面低端距离
    document.sections[0].header_distance = Cm(1.5) 
    document.sections[0].footer_distance = Cm(1.75)
    header = document.sections[0].header # 获取第一个节的页眉(所有的页眉都一致)
    
    
    paragraph = header.paragraphs[0] # 获取页眉的文字part
    text=paragraph.add_run('my header')
    text.font.size = Pt(10)                # 页眉字体大小
    text.bold = True                    # 页眉字体是否加粗
    text.font.name = 'Arial'           # 控制是英文时的字体
    text.element.rPr.rFonts.set(qn('w:eastAsia'), '宋体')  # 控制是中文时的字体
    paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER ##设置页眉居中



    ##在页眉中插入表格
    table_h = header.add_table(rows=1, cols=3,width=Inches(2))##一行3列的表格 
    ##设置页眉中表格的行高
    table_h.rows[0].height=Cm(0.6)
    ##在表格中填入文字
    run = table_h.cell(0,0).paragraphs[0].add_run(u'名称Title: ')
    run.font.name = 'Arial'
    run.font.size = Pt(10)
    run.italic = True ##设置是否为斜体
    r = run._element 
    r.rPr.rFonts.set(qn('w:eastAsia'), '宋体')
    ##第一个cell中的两段文字 字体设置不一样,所以需要分开来写
    run1 = table_h.cell(0,0).paragraphs[0].add_run(u'firsrt cell text')
    run1.font.name = 'Arial' ##英文字体
    run1.font.size = Pt(10)  ##字体大小
    run1.bold = True  ##加粗
    r1 = run1._element 
    r1.rPr.rFonts.set(qn('w:eastAsia'), '宋体') ##中文字体

    
    run2 = table_h.cell(0,1).paragraphs[0].add_run(u'second cell text:')
    run2.font.name = 'Arial'
    run2.font.size = Pt(10)
    run2.italic = True
    r2 = run2._element 
    r2.rPr.rFonts.set(qn('w:eastAsia'), '宋体')
    
    run2 = table_h.cell(0,2).paragraphs[0].add_run(u'third cell text')
    run2.font.name = 'Arial'
    run2.font.size = Pt(10)
    run2.bold = True
    r2 = run2._element 
    r2.rPr.rFonts.set(qn('w:eastAsia'), '宋体')
    
    ##对页眉表格中的文字整体居中        
    for i in range(0,3):                      
        table_h.cell(0,i).vertical_alignment = WD_ALIGN_VERTICAL.CENTER
        for par in table_h.cell(0,i).paragraphs:   
            ##horizontal centered
            par.paragraph_format.alignment = WD_TABLE_ALIGNMENT.LEFT                         #            
            # for run in par.runs:                    
            #     run.font.size = Pt(10)
            #     r = run._element                    
            #     run.font.name ='Arial'                    
            #     r.rPr.rFonts.set(qn('w:eastAsia'), '宋体')
    for par in table_h.cell(0,2).paragraphs:               
        par.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER
    
    ##调整页眉表格的列宽 每列数字相加为10
    col_width_dic = {0: 7, 1: 2, 2: 1}
    for col_num in range(0,3):    
        table_h.cell(0, col_num).width = Inches(col_width_dic[col_num])
    ##加边框    
    table_h.style='Table Grid'

4. 插入页脚(页码)

#页脚为页码
##add_page_number借鉴了别人写的插入页码function
add_page_number(document.sections[0].footer.paragraphs[0])
document.sections[0].footer.paragraphs[0].alignment =WD_ALIGN_PARAGRAPH.RIGHT

5.插入一个空白表格

table = document.add_table(rows=rows, cols=10,style='Table Grid')
##为表格赋值
table.cell(i,j).text=value

6. 调整表格格式

    ##设置行高
    table.rows[0].height=Cm(1.06)
    table.rows[1].height=Cm(1.06)
    table.rows[2].height=Cm(1.06)
    table.rows[3].height=Cm(2.21)
    for i in range(4,rows):
         table.rows[i].height=Cm(0.8)
    ##设置列宽

    col_width_dic = {0:0.3,1:1.7,2:1.1,3:1.4,4:0.5,5:1.5,6:0.6,7:1.5,8:0.7,9:0.7}
    for col_num in range(0,10):    
        table.cell(3, col_num).width = Inches(col_width_dic[col_num])
    

7. 合并单元格+表格中字体格式设置

##先合并单元格再在单元格中赋值
table.cell(0,0).merge(table.cell(0,1))
table.cell(0,0).text="名称"
##表格内容居中&字体
for i in range(len(table.rows)):              
        for j in range(len(table.columns)):                      
            table.cell(i,j).vertical_alignment = WD_ALIGN_VERTICAL.CENTER
            for par in table.cell(i,j).paragraphs:   
                ##horizontal centered
                par.paragraph_format.alignment =WD_TABLE_ALIGNMENT.CENTER                         #            
                for run in par.runs:                    
                    run.font.size = Pt(10)
                    r = run._element                    
                    run.font.name ='Arial'                    
                    run.font.name ='宋体'  
                    r.rPr.rFonts.set(qn('w:eastAsia'), '宋体')
                    run.font.bold=True
                    

8.保存word

document.save('table.docx')

add page number 

def create_element(name):
    return OxmlElement(name)

def create_attribute(element, name, value):
    element.set(ns.qn(name), value)


def add_page_number(paragraph):
    paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
    page_run = paragraph.add_run()
    t1 = create_element('w:t')
    create_attribute(t1, 'xml:space', 'preserve')
    t1.text = ''
    # t1.text = 'Page '
    page_run._r.append(t1)

    page_num_run = paragraph.add_run()

    fldChar1 = create_element('w:fldChar')
    create_attribute(fldChar1, 'w:fldCharType', 'begin')

    instrText = create_element('w:instrText')
    create_attribute(instrText, 'xml:space', 'preserve')
    instrText.text = "PAGE"

    fldChar2 = create_element('w:fldChar')
    create_attribute(fldChar2, 'w:fldCharType', 'end')

    page_num_run._r.append(fldChar1)
    page_num_run._r.append(instrText)
    page_num_run._r.append(fldChar2)

    of_run = paragraph.add_run()
    t2 = create_element('w:t')
    create_attribute(t2, 'xml:space', 'preserve')
    # t2.text = ' of '
    t2.text="/"
    of_run._r.append(t2)

    fldChar3 = create_element('w:fldChar')
    create_attribute(fldChar3, 'w:fldCharType', 'begin')

    instrText2 = create_element('w:instrText')
    create_attribute(instrText2, 'xml:space', 'preserve')
    instrText2.text = "NUMPAGES"

    fldChar4 = create_element('w:fldChar')
    create_attribute(fldChar4, 'w:fldCharType', 'end')

    num_pages_run = paragraph.add_run()
    num_pages_run._r.append(fldChar3)
    num_pages_run._r.append(instrText2)
    num_pages_run._r.append(fldChar4)

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐