zoukankan      html  css  js  c++  java
  • python-docx模块的知识总结

    问题1:python-docx指定页面设置成横向?

    python-docx包的add_section()可以添加一个新的节,section.orientation、section.page_width、section.page_height属性可以改变节纸张的方向。关于这些属性和方法的详细用法以及页面设置的内容可以参考文章python-docx页面设置,python-docx节的添加、定位和分节符的设置。

    那么,我们采用第一部分的步骤尝试使用python实现改变部分页面的纸张方向,代码如下:

    from docx import Document
    from docx.enum.section import WD_ORIENTATION, WD_SECTION_START # 导入节方向和分解符类型
    document = Document() # 新建docx文档
    document.add_paragraph() # 添加一个空白段落
    section = document.add_section(start_type=WD_SECTION_START.CONTINUOUS) # 添加横向页的连续节
    section.orientation = WD_ORIENTATION.LANDSCAPE # 设置横向
    page_h, page_w = section.page_width, section.page_height
    section.page_width = page_w # 设置横向纸的宽度
    section.page_height = page_h # 设置横向纸的高度
    document.add_paragraph() # 添加第二个空白段落
    section = document.add_section(start_type=WD_SECTION_START.CONTINUOUS) # 添加连续的节
    section.orientation = WD_ORIENTATION.PORTRAIT # 设置纵向
    page_h, page_w = section.page_width, section.page_height # 读取插入节的高和宽
    section.page_width = page_w # 设置纵向纸的宽度
    section.page_height = page_h # 设置纵向纸的高度
    document.save('test.docx')

    引用:https://baijiahao.baidu.com/s?id=1665028380582245274

    问题2:python给word文档添加标题

    mport docx
    doc=docx.Document()
    #整数 0 表示标题是 Title 样式,这用于文档的顶部。整数 1 到 45是不同的标题层次,是主要的标题, 45是最低层的子标题
    doc.add_heading('标题0',0)
    doc.add_heading('标题1',1)
    doc.add_heading('标题2',2)
    doc.add_heading('标题3',3)
    doc.add_heading('标题4',4)
    doc.add_heading('标题5',5)
    doc.save('example3.docx')

    引用:https://www.cnblogs.com/shunguo/p/11399305.html

    问题3:word插入日期,格式为年月日,且居中显示

    # doc = Document() #新建word文件
    import time
    today=time.strftime("%Y-%m-%d",time.localtime())
    date_para=doc.add_paragraph()
    
    # date_run=date_para.add_run(today)   #2020-08-13
    # print(today[0:5])
    date_run=date_para.add_run("%s年%s月%s日"%(today[0:4],today[6:7],today[8:11]))
    date_run.font.name=u"宋体"
    date_run.font.size=Pt(16)
    
    date_para.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER     #日期居中
    doc.add_page_break()  #插入分页符

    问题4:word插入表格,表格里面插入图片,图片下面显示照片名称且居中显示

    
    
    from docx import Document
    from docx.enum.text import WD_PARAGRAPH_ALIGNMENT #用作设置段落对齐 WD_ALIGN_PARAGRAPH
    from docx.shared import Pt #磅数
    from docx.oxml.ns import qn #中文格式
    import time,os,sys,datetime

    import docx #导入docx库
    from docx.shared import Inches #图片尺寸
    from docx.enum.table import WD_TABLE_ALIGNMENT #表格内容居中
    # doc = Document("./照片管理模板.docx") #打开word文件
    doc = Document() #新建word文件


    doc.add_heading('标题0',0) # myDoucment.add_heading(fn.strip('-0.jpg'), level=1) # 插入图片名称,作为一级标题生成目录 table=doc.add_table(rows=4,cols=3, style="Normal Table") # table=doc.add_table(rows=4,cols=3, style="Table Grid") # table.cell(0,0).add.picture("1.JPG") #设置表格内容字体和大小 # table.style.font.name=u"楷体" # table.style.font.size = 20 ######################## #读取照片信息 # table.cell(1,0).text='第1张照片' run = table.cell(1, 0).paragraphs[0].add_run("第1张照片") run.font.name = u'黑体' run.font.size = Pt(12) table.cell(1,1).text='第2张照片' table.cell(1,2).text='第3张照片' table.cell(3,0).text='第4张照片' table.cell(3,1).text='第5张照片' table.cell(3,2).text='第6张照片' w = float(8 / 2.54) # cm转为英寸 h = float(6 / 2.54) # cm转为英寸 run = doc.tables[0].cell(0, 0).paragraphs[0].add_run() run.add_picture("1.JPG",width=Inches(w),height=Inches(h)) run = doc.tables[0].cell(0, 1).paragraphs[0].add_run() run.add_picture("1.JPG",width=Inches(w),height=Inches(h)) run = doc.tables[0].cell(0, 2).paragraphs[0].add_run() run.add_picture("1.JPG",width=Inches(w),height=Inches(h)) run = doc.tables[0].cell(2, 0).paragraphs[0].add_run() run.add_picture("1.JPG",width=Inches(w),height=Inches(h)) run = doc.tables[0].cell(2, 1).paragraphs[0].add_run() run.add_picture("1.JPG",width=Inches(w),height=Inches(h)) run = doc.tables[0].cell(2, 2).paragraphs[0].add_run() run.add_picture("1.JPG",width=Inches(w),height=Inches(h)) #设置表格内容居中 for r in range(4):#循环将每一行,每一列都设置为居中 for c in range(3):
        table.cell(r, c).paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER
     #插入分页符 doc.add_page_break()

     

    因为不懂,所以要学; 因为平凡,所以努力。 因为有为,所以有位。 因为执着,所以精彩。
  • 相关阅读:
    Play 中如何使用 Ajax
    Play!中使用HTTP异步编程
    Asynchronous Jobs
    Play libs
    JPA persistence
    maven 打包和构建的Linux命令(mvn)
    Istio的流量管理入门-charlieroro编写
    Linux和Docker的Capabilities介绍及Setcap命令
    2020超实用提升英文阅读能力和必备3000单词表
    Cookie什么?Cookie和Session防御怎么做?
  • 原文地址:https://www.cnblogs.com/quezesheng/p/13497275.html
Copyright © 2011-2022 走看看