• 67.python操作word文档


    1.python操作word文档:

    import docx
    from docx.oxml.ns import qn
    from docx.shared import Pt, RGBColor, Inches
    from docx.enum.text import WD_PARAGRAPH_ALIGNMENT as WD_ALIGN_PARAGRAPH
    
    # 读取文档
    # file = docx.Document("demo.docx")
    # print("段落数:"+str(len(file.paragraphs)))#段落数为6,每个回车隔离一段
    # for para in file.paragraphs:
    #     print(para.text)
    #
    # for i in range(len(file.paragraphs)):
    #     print("第"+str(i)+"段的内容是:"+file.paragraphs[i].text)
    #####################
    # 制作文档
    #####################
    
    doc = docx.Document()
    
    # 设置文档字体
    doc.styles['Normal'].font.name = u'微软雅黑'
    doc.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'微软雅黑')
    # doc.styles['Normal'].font.size = Pt(12)
    
    
    # 添加文字并设置居中
    # doc.add_heading("琅琊榜", level=0)
    p = doc.add_paragraph()  # 生成段
    run = p.add_run("琅琊榜")  # 通过run添加文字
    run.font.size = Pt(26)  # 文字大小
    run.bold = True
    run.font.color.rgb = RGBColor(255, 0, 0)
    p.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER  # 文字居中
    
    # 添加图片,并设置大小
    pic = doc.add_picture(r'C:UsersyztPictures	img.jpg', width=Inches(5))
    last_paragraph = doc.paragraphs[-1]
    last_paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER  # 图片居中
    
    # 文字缩进
    p = doc.add_paragraph()
    run = p.add_run(
        '梅长苏(胡歌饰)本远在江湖,却名动帝辇。江湖传言:“江左梅郎,麒麟之才,得之可得天下。”作为天下第一大帮“江左盟”的首领,梅长苏“梅郎”之名响誉江湖。然而,有着江湖至尊地位的梅长苏,却是一个病弱青年、弱不禁风,背负着十多年前巨大的冤案与血海深仇,就连身世背后也隐藏着巨大的秘密。')
    p_format = p.paragraph_format
    p_format.first_line_indent = Inches(0.2)
    
    # 表格
    doc.add_paragraph(text="
    ", style=None)
    p = doc.add_paragraph()
    run = p.add_run("音乐原声")
    run.font.size = Pt(22)
    run.bold = True
    
    table = doc.add_table(rows=4, cols=5)
    table.style = doc.styles['Table Grid']  # 表格样式
    # table.cell(0, 0).text = "歌曲"
    # table.cell(0, 1).text = "演唱者"
    # table.cell(0, 2).text = "作词"
    # table.cell(0, 3).text = "作曲"
    # table.cell(0, 4).text = "类型"
    
    
    def th(x, y, content):
        """
        th样式
        :param x: x坐标
        :param y: y坐标
        :param content: 内容
        :return: None
        """
        p = table.cell(x, y).paragraphs[0]
        p.alignment = WD_ALIGN_PARAGRAPH.CENTER
        run = p.add_run(content)
        run.bold = True  # 加粗
    
    
    th(0, 0, "歌曲")
    th(0, 1, "演唱者")
    th(0, 2, "作曲")
    th(0, 3, "作词")
    th(0, 4, "类型")
    
    
    def td_read(table, x, y, content):
        """
            td红色字体
            :param table: 表格对象
            :param x: x坐标
            :param y: y坐标
            :param content: 内容
            :return: None
            """
        run = table.cell(x, y).paragraphs[0].add_run(content)
        run.font.size = Pt(11)
        run.font.color.rgb = RGBColor(255, 0, 0)
    
    
    table.cell(1, 0).text = "《风起时》"
    # table.cell(1, 1).text = "胡歌"
    td_read(table, 1, 1, "胡歌")
    table.cell(1, 2).text = "孟可"
    table.cell(1, 3).text = "海宴"
    table.cell(1, 4).text = "主题曲、片尾曲"
    
    table.cell(2, 0).text = "《红颜旧》"
    table.cell(2, 1).text = "刘涛"
    table.cell(2, 2).text = "赵佳霖"
    table.cell(2, 3).text = "袁亮"
    table.cell(2, 4).text = "插曲"
    
    table.cell(3, 0).text = "《赤血长殷》"
    table.cell(3, 1).text = "王凯"
    table.cell(3, 2).text = "于海航"
    table.cell(3, 3).text = "清彦、冰封"
    table.cell(3, 4).text = "插曲"
    
    doc.save('ceshi.docx')
    
    参考:
        https://www.cnblogs.com/xiao987334176/p/9995976.html#autoid-0-0-0
        https://www.cnblogs.com/z123zero/p/10770097.html
    
  • 相关阅读:
    【习题 3-12 UVA
    【习题 3-9 UVA
    【Codeforces Round #299 (Div. 2) E】Tavas and Pashmaks
    分布式ID生成器的解决方案总结
    Spring MVC表单防重复提交
    Spring import配置文件使用占位符
    什么是灰度发布,灰度测试。
    浅析负载均衡的6种算法,Ngnix的5种算法。
    神器,阿里巴巴Java代码检查插件
    去BAT面试完的Mysql面试题总结(55道,带完整答案)
  • 原文地址:https://www.cnblogs.com/liuzhanghao/p/13253304.html
Copyright © 2020-2023  润新知