码迷,mamicode.com
首页 > 编程语言 > 详细

67.python操作word文档

时间:2020-07-06 10:51:25      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:content   音乐   turn   alt   rap   from   天下第一   har   element   

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:\Users\yzt\Pictures\timg.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="\r", 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

67.python操作word文档

标签:content   音乐   turn   alt   rap   from   天下第一   har   element   

原文地址:https://www.cnblogs.com/liuzhanghao/p/13253304.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!