Python–常用模块脚本–word读写

#.word读写

文章目录

from docx import Document
from docx.shared import Cm, Inches, Pt, RGBColor
from docx.oxml.ns import qn
from docx import shared
from docx.enum.style import WD_STYLE_TYPE
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.enum.table import WD_TABLE_ALIGNMENT, WD_CELL_VERTICAL_ALIGNMENT

def simple_paragragh():
    doc = Document()  # 创建内存中的word文档对象
    doc.add_paragraph("这是第一段")  # 写入若干段落
    doc.add_paragraph("这是第二段")
    doc.add_picture('img.png', width=shared.Cm(10))  # 插入图片,设置宽度为10cm
    doc.paragraphs[-1].alignment = WD_PARAGRAPH_ALIGNMENT.CENTER  # 设置最后一段居中对齐,这里可以使图片居中
    doc.save(r"test.docx")  # 保存才能看到结果

def simple_font():
    doc = Document()
    p1 = doc.add_paragraph()
    text1 = p1.add_run("第一段文字是中文;The first paragraph is in Chinese")
    text1.font.size = Pt(15)  # 设置字体大小
    text1.bold = True  # 设置字体是否加粗
    text1.font.name = 'Times New Roman'  # 设置new Roman
    text1.element.rPr.rFonts.set(qn('w:eastAsia'), '宋体')  # 设置中文字体

    #doc.add_page_break()  #增加分页符

    p2 = doc.add_paragraph()
    # 段落对齐方式,'LEFT','CENTER','RIGHT','JUSTIFY',DISTRIBUTE...
    p2.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.JUSTIFY
    text2 = p2.add_run("第二段文字是英文;The second paragraph is in English")
    text2.font.size = Pt(10)
    text2.bold = False
    text2.font.name = 'Times New Roman'
    text2.element.rPr.rFonts.set(qn('w:eastAsia'), '黑体')

    p3 = doc.add_paragraph()
    p3.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.JUSTIFY #两端对齐
    # p3.paragraph_format.left_indent = Inches(0.5)  #左缩进,0.5英寸
    # p3.paragraph_format.right_indent = Pt(20)  #右缩进,20磅
    p3.paragraph_format.first_line_indent = Pt(21)  #首行缩进
    p3.paragraph_format.line_spacing = 1.5  #行间距 1.5倍
    p3.paragraph_format.space_before = 0  #段前间距
    p3.paragraph_format.space_after = 0  #段后间距

    text3 = p3.add_run("第三段文字是测试段落,为了测试左缩进,还要去测试右缩进,"
                       "第三段文字是测试段落,为了测试左缩进,还要去测试右缩进,"
                       "第三段文字是测试段落,为了测试左缩进,还要去测试右缩进,")
    # 五号字体 = 10.5pt = 3.70mm = 14px = 0.146inch
    text3.font.size = Pt(10.5)  # 设置字体大小 5号字体
    text3.bold = False  # 设置字体是否加粗
    text3.font.italic = False  #是否斜体
    text3.font.name = 'Times New Roman'  # 设置new Roman
    text3.element.rPr.rFonts.set(qn('w:eastAsia'), '宋体')  # 设置中文字体
    # text3.font.color.rgb = RGBColor(255, 55, 55)  # 红色
    # text3.font.underline = True  # 设置下划线
    # text3.font.outline = True  # 设置轮廓线
    # text3.font.shadow = True  # 设置阴影
    # text3.font.strike = True  # 删除线
    # text3.font.double_strike = True  # 双删除线
    # text3.font.subscript = True  # 设置下标
    # text3.font.superscript = True  # 设置上标

    # for s in doc.styles:  #得到表的样式
    #     if s.type == WD_STYLE_TYPE.TABLE:
    #         print(s.name)

    doc.save('test.docx')

def simple_table():
    doc = Document()
    cols = 11  # 表格的总列数
    table = doc.add_table(rows=1, cols=cols, style='Table Grid')
    table.alignment = WD_TABLE_ALIGNMENT.CENTER

    # 写标题行,并设置字体
    hdr_cells = table.rows[0].cells
    Fields = ['序号', '字段', '日期', 'x1', 'y1', 'x2', 'y2', '重要性', '整数', '类型', '备注']
    for i in range(cols):
        hdr_cells[i].paragraphs[0].paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
        run = hdr_cells[i].paragraphs[0].add_run(Fields[i])
        run.font.name = u'宋体'
        run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
        run.font.bold = True
        if i == 2:
            hdr_cells[i].width = Cm(2.7)

    f1 = open('test.txt')
    rows = 0  # 填写记录的数量
    for line in f1:
        # 添加表格
        row_cells = table.add_row().cells
        array = list(map(eval, line.split(",")))
        rows += 1
        strs = []
        for i in range(cols):
            if i == 0:
                strs.append(str(rows))
            elif i < 3:
                strs.append('')
            elif i < 7:
                strs.append(str(array[i - 3]))
            elif i == 7:
                strs.append('正常')
            elif i == 8:
                strs.append('10')
            elif i == 9:
                strs.append('P')
            elif i == 10:
                strs.append('')
        for i in range(cols):
            row_cells[i].paragraphs[0].paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
            run = row_cells[i].paragraphs[0].add_run(strs[i])
            run.font.name = u'宋体'
            run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
    f1.close()

    # 合并单元格
    table.cell(1, 1).merge(table.cell(rows, 1))
    table.cell(1, 1).text = ''
    table.cell(1, 1).paragraphs[0].paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
    run = table.cell(1, 1).paragraphs[0].add_run('B')
    run.font.name = u'宋体'
    run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
    table.cell(1, 1).vertical_alignment = WD_CELL_VERTICAL_ALIGNMENT.CENTER

    table.cell(1, 2).merge(table.cell(int(rows / 2), 2))
    table.cell(1, 2).text = ''
    table.cell(1, 2).paragraphs[0].paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
    run = table.cell(1, 2).paragraphs[0].add_run('2020-3-24')
    run.font.name = u'宋体'
    run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
    table.cell(1, 2).vertical_alignment = WD_CELL_VERTICAL_ALIGNMENT.CENTER

    table.cell(int(rows / 2 + 1), 2).merge(table.cell(rows, 2))
    table.cell(int(rows / 2 + 1), 2).text = ''
    table.cell(int(rows / 2 + 1), 2).paragraphs[0].paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
    run = table.cell(int(rows / 2 + 1), 2).paragraphs[0].add_run('2020-3-25')
    run.font.name = u'宋体'
    run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
    table.cell(int(rows / 2 + 1), 2).vertical_alignment = WD_CELL_VERTICAL_ALIGNMENT.CENTER

    doc.save('test.docx')

if __name__ == '__main__':
    simple_table()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柴寺仓

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值