当前位置: 代码迷 >> python >> Python PPTX table.cell颜色
  详细解决方案

Python PPTX table.cell颜色

热度:92   发布时间:2023-06-16 14:01:48.0

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

这是我的代码:

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)

我从项目一个得到了这段代码。 可悲的是,我不知道如何使用此库更改边框颜色和宽度。

  相关解决方案