Python PPTX table.cell 颜色

标签 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

最佳答案

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

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)

我从 issue on the github page 中得到这段代码的项目。遗憾的是,我不知道如何使用此库更改边框颜色和宽度。

关于Python PPTX table.cell 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42272447/

相关文章:

python - 如何在python中编辑图表轴 pptx - 'GraphicFrame'对象没有属性 'category_axis'

python - 如何在python pptx中更改标题颜色

python - 无法理解带有 ?= 的 python 正则表达式

python - 如果我取随机整数,如何除无余数?

python - 在 urls.py 中使用正则表达式的内联过滤器

python PPTX : How to Update or Replace a Chart Series?

python - 'can' t 分配给文字'意味着什么?

python - TensorFlow 中的 Seq2Seq(无嵌入)

python-3.x - 通过 Python 更新 PowerPoint 中链接的 Excel 路径

python - 如何使用 pptx python 库更改 PPT 幻灯片上项目符号列表的缩进