Python PPTX table.cell颜色

时间:2017-02-16 11:18:20

标签: python python-pptx

所以伙计们这里是我的问题我想用默认的颜色填充我的表格单元格...我已经检查了文档并在谷歌上进行了多次搜索但却找不到有用的东西。

这是我的代码:

def create_default_slide(user, ppt, shapes, experience_text, skills):
    max_height = Inches(ppt.slide_height.inches - kBASE_BOTTOM_INCHES)
    height = Inches(kBASE_TOP_INCHES)
    left = Inches(0)
    top = Inches(.1)
    shapes.add_picture(kASSETS_DIRECTORY + "ppt_softinsa_header.png",
                       left, top,
                       height=height,
                       width=ppt.slide_width)


    # shapes.title.text = "curriculum vitae – Resource {}".format(1)
    title_box = shapes.add_textbox(left=Inches(0.5),
                                   top=Inches(kBASE_TOP_INCHES * 1.5),
                                   width=ppt.slide_width, height=Pt(px_to_pt(50)))

    title_box.text = u'Curriculum Vitae – {}'.format(user.name)

    info_table = shapes.add_table(rows=3, cols=2,
                                  left=Inches(.2), top=Inches(kBASE_TOP_INCHES * 3),
                                  width=200, height=200).table

    # set table properties
    info_table.first_row = False
    info_table.horz_banding = False
    info_table.vert_banding = True

    # set column widths
    info_table.columns[0].width = Inches(1.4)
    info_table.columns[1].width = Inches(3)

    rows_number = len(info_table.rows)

    user_info = user.basic_info()
    for i in range(rows_number):
        info_table.cell(i, 0).text = kINTRODUCTION_COLUMN[i]
        info_table.cell(i, 1).text = user_info[i]

        # sets the font size for the content info of the table
        info_cell = info_table.rows[i].cells[1]
        info_cell.text_frame.paragraphs[0].font.size = Pt(kCELL_INFO_FONT_SIZE)

    experiences_table = shapes.add_table(rows=2, cols=1,
                                         left=Inches(5), top=Inches(kBASE_TOP_INCHES * 3),
                                         width=200, height=Inches(9.9).).table

    # set table dimensions
    experiences_table.columns[0].width = Inches(4.7)
    experiences_table.rows[0].height = Inches(kTABLE_HEADER_INCHES)

    # set cell font size
    experience_title_cell = experiences_table.rows[0].cells[0]
    experience_cell = experiences_table.rows[1].cells[0]



    experience_cell.text_frame.paragraphs[0].font.size = Pt(kCELL_INFO_FONT_SIZE)

    # set header
    # "Professional Experience"
    experiences_table.cell(0, 0).text = u"Experiência Profissional"


    import re

    expr = re.compile(ur'- .+ até [^\n]+\n')

    for experience_item in experience_text:
        if expr.search(experience_item):
            lines = experience_item.split('\n')

            paragraph = experiences_table.cell(1, 0).text_frame.paragraphs[0]
            bold_run = paragraph.add_run()

            bold_run.font.bold = True

            bold_run.text = lines[0] + '\n'

            rest_run = paragraph.add_run()

            rest_run.font.bold = False

            rest_run.text = '\n'.join(lines[1:]) + '\n'
        else:
            experiences_table.cell(1, 0).text = '\n'.join(experience_text)

    education_table = shapes.add_table(rows=2, cols=1,
                                       left=Inches(.2), top=Inches(kBASE_TOP_INCHES * 5.5),
                                       width=200, height=Inches(3.2)).table

    # set column widths
    education_table.columns[0].width = Inches(4.4)
    education_table.rows[0].height = Inches(kTABLE_HEADER_INCHES)

    # set header title
    education_table.cell(0, 0).text = "Formação"

    # set font size for table info
    education_cell = education_table.rows[1].cells[0]
    education_cell.text_frame.paragraphs[0].font.size = Pt(kCELL_INFO_FONT_SIZE)

    user_education = user.education_info()
    education_info = []

    skills_table = shapes.add_table(rows=2, cols=1,
                                       left=Inches(.2), top=Inches(kBASE_TOP_INCHES * 9.5),
                                       width=200, height=Inches(3.3)).table

    # set column widths
    skills_table.columns[0].width = Inches(4.4)
    skills_table.rows[0].height = Inches(kTABLE_HEADER_INCHES)

    # set header title
    skills_table.cell(0, 0).text = "Competências"

    # set font size for table info
    skills_cell = skills_table.rows[1].cells[0]
    skills_cell.text_frame.paragraphs[0].font.size = Pt(kCELL_INFO_FONT_SIZE)

    skills_table.cell(1, 0).text = "".join(skills)

    # TODO: check if it always on object or if it can be a list
    for course in user_education['courses']:
        education_info.append(
            u'{} de {}'.format(
                DEGREE_LEVELS[course['degree']] if course['degree'] else course['degree'],
                course['name']
            )
        )

    user_certifications = user_education['certifications']
    if len(user_certifications) is not 0:
        education_info.append(
            u'Certificações: {}'.format(u', '.join(user_certifications))
        )

    bullets = ""
    for i in range(len(education_info)):
        bullets += u'- {}\n'.format(education_info[i])

    education_table.cell(1, 0).text = bullets

    text_box = shapes.add_textbox(left=Inches(0),
                                  top=Inches(ppt.slide_height.inches - kBASE_BOTTOM_INCHES),
                                  width=ppt.slide_width, height=Pt(px_to_pt(50)))

    # text_box.text = "Proposta Nº{} - Confidencial".format("P63838/1")
    p = text_box.text_frame.add_paragraph()
    p.text = u'Confidencial'  # "Proposta Nº{} - Confidencial".format("P63838/1")
    p.alignment = PP_PARAGRAPH_ALIGNMENT.CENTER
    p.font.size = Pt(8)

    shapes.add_picture(kASSETS_DIRECTORY + "ppt_footer.png",
                       left=Inches(ppt.slide_width.inches - 2.5),
                       top=Inches(ppt.slide_height.inches - (kBASE_BOTTOM_INCHES / 2)),
                       height=Pt(px_to_pt(10)),
                       width=Pt(px_to_pt(185)))

    return shapes

1 个答案:

答案 0 :(得分:8)

这段代码设置表格中单个单元格的颜色:

usings

我从项目的issue on the github page获得了这段代码。可悲的是,我不知道如何使用这个库更改边框颜色和宽度。