Python PPTX table.cell color

我是研究僧i 提交于 2020-07-18 18:11:56

问题


So guys here's my problem I would like to fill my table cells with different colors from the default ones... I've checked the docs and made multiple searches on google but couldn´t find something helpful.

Here's my code:

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:


This piece of code sets the color of a single cell in a table:

from pptx.dml.color import RGBColor

# cell is a table cell
# set fill type to solid color first
cell.fill.solid()

# set foreground (fill) color to a specific RGB color
cell.fill.fore_color.rgb = RGBColor(0xFB, 0x8F, 0x00)

I got this piece of code from an issue on the github page of the project. Sadly I don't know how to change the border color and width using this library.



来源:https://stackoverflow.com/questions/42272447/python-pptx-table-cell-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!