获取用python_docx下划线的表行

时间:2019-02-28 22:34:46

标签: python python-docx

我想用 python-docx 带下划线的表格行,所以我遵循了this answer,但是我将行中的每个单词分别加了下划线:

enter image description here

我要在整行下划线,如下所示:

enter image description here

这是相关代码:

    document = Document()

    section = document.sections[-1]
    section.left_margin = Inches(0.5)
    section.right_margin = Inches(0.5)

    style = document.styles['Normal']
    font = style.font
    font.name ='Arial'
    font.size = Pt(10)

    document.add_heading("COMPANY")
    document.add_paragraph("Operación de {} del día {}".format(operation_type[context['operation_type_id']], context['operation_date']))
    document.add_paragraph(weekdays[str(self.get_operation_date().weekday())])

    transfers = context['transfers']

    table = document.add_table(rows=len(transfers), cols=10)
    hdr_cells = table.rows[0].cells
    run = []
    run.append(hdr_cells[0].paragraphs[0].add_run('Booking'))
    run[0].underline = True
    run[0].font.size = Pt(8)
    run.append(hdr_cells[1].paragraphs[0].add_run('Nombre'))
    run[1].underline = True
    run[1].font.size = Pt(8)
    run.append(hdr_cells[2].paragraphs[0].add_run('#'))
    run[2].underline = True
    run[2].font.size = Pt(8)
    run.append(hdr_cells[3].paragraphs[0].add_run('Vuelo'))
    run[3].underline = True
    run[3].font.size = Pt(8)
    run.append(hdr_cells[4].paragraphs[0].add_run('Hr'))
    run[4].underline = True
    run[4].font.size = Pt(8)
    run.append(hdr_cells[5].paragraphs[0].add_run('P Up'))
    run[5].underline = True
    run[5].font.size = Pt(8)
    run.append(hdr_cells[6].paragraphs[0].add_run('Traslado'))
    run[6].underline = True
    run[6].font.size = Pt(8)
    run.append(hdr_cells[7].paragraphs[0].add_run('Circuito'))
    run[7].underline = True
    run[7].font.size = Pt(8)
    run.append(hdr_cells[8].paragraphs[0].add_run('Priv?'))
    run[8].underline = True
    run[8].font.size = Pt(8)
    run.append(hdr_cells[9].paragraphs[0].add_run('Agencia'))
    run[9].underline = True
    run[9].font.size = Pt(8)


    current_directory = settings.MEDIA_DIR
    file_name = "Operaciones {} {}.docx".format(operation_type[context['operation_type_id']], context['operation_date'])
    document.save("{}/{}".format(current_directory, file_name))

有什么方法可以通过 python-docx 实现此目标吗?

0 个答案:

没有答案