栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 操作输出world报告

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 操作输出world报告

 标题格式
        document = Document()  # 新建文档对象
        creatFileTime = time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime())
        filePith = ".报告"+creatFileTime+".docx"#报告文件路径和名称
        print("报告位置和名称"+filePith)
        # 全局指定字体
        document.styles['Normal'].font.name = u'.宋体'
        document.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'.宋体')

        header = document.add_heading('', level=1)#设置标题
        _header = header.add_run("电网报告")
        # 设置西文字体
        _header.font.name = u'宋体'
        # 设置中文字体
        _header._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
        # 设置字体颜色
        _header.font.color.rgb = RGBColor(0, 0, 0)  # 黑色
        # 设置字体大小
        _header.font.size = Pt(24)
        # 设置下划线
        _header.font.underline = False
        # 设置删除线
        _header.font.strike = False
        # 设置加粗
        _header.bold = True
        # 设置斜体
        _header.italic = False

        # 设置标题对齐方式为居中对齐
        header_format = header.paragraph_format
        header_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER

        # 保存文档
        print(filePith)
        document.save(filePith)  # 保存文档

使用run.font.size = Pt(30)设置字体大小,Pt表示磅,与字号对应关系如下:

号数磅值 (Pt)
初号42
小初36
一号26
小一24
二号22
小二18
三号16
小三15
四号14
小四12
五号10.5
小五9
六号7.5
小六6.5
七号5.5
八号5

设置页面纸张大小和边距

       section = self.document.sections[0]
        # 修改页面高、宽,单位厘米
        section.page_width = Cm(21)
        section.page_height = Cm(29.7)
        # 修改页面上下左右边距,单位厘米
        section.top_margin = Cm(2.12)
        section.bottom_margin = Cm(1.94)
        section.left_margin = Cm(0.74)
        section.right_margin = Cm(0.74)
对齐、间距、行距
from docx import Document
from docx.shared import Pt, Inches
from docx.oxml.ns import qn
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT

d = Document()
d.styles['Normal'].font.size = Pt(20)
d.styles['Normal'].font.name = u'Times New Roman'
d.styles['Normal'].element.rPr.rFonts.set(qn('w:eastAsia'), '宋体')
p = d.add_paragraph("xxxx报告")
# 设置对齐格式
# 可选项有LEFT、CENTER、RIGHT、JUSTIFY、DISTRIBUTE等
p.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT
# 设置段落缩进
d.add_paragraph("右缩进,0.5英寸寸寸寸寸寸寸寸寸寸寸寸寸寸寸寸寸寸寸寸").paragraph_format.right_indent = Inches(0.5)
d.add_paragraph("左缩进,20磅").paragraph_format.left_indent = Pt(20)
d.add_paragraph("首行缩进2个字符").paragraph_format.first_line_indent = Pt(20) * 2
# 设置段落间距 1.5倍行间距 1.5, 单倍行间距 1.0
d.add_paragraph("line_spacing设置为浮点数,表示行间距为行高的倍数").paragraph_format.line_spacing = 1.5
d.add_paragraph("line_spacing设置为具体的长度值,表示绝对距离").paragraph_format.line_spacing = Pt(50)
d.add_paragraph("space_before表示段前间距").paragraph_format.space_before = Pt(100)
d.add_paragraph("space_after表示段后间距").paragraph_format.space_after = Pt(30)
# 设置段落内部文字在遇到需分页情况时处理状态
p2 = d.add_paragraph("很长的段落很长的段落很长的段落很长的段落很长的段落很长的段落很长的段落很长的段落很长的段落很长的段落很长的段落")
p2.paragraph_format.keep_together = True  # 段中不分页
p2.paragraph_format.keep_with_next = True  # 与下段同页
p2.paragraph_format.page_break_before = True  # 段前分页
p2.paragraph_format.widow_control = True  # 孤行控制
d.save("11111.docx")

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/950068.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号