[Python] Xlsxwriter custom data labels with custom data label positions

Discussão em 'Python' iniciado por Stack, Agosto 4, 2025.

  1. Stack

    Stack Membro Participativo

    We're trying to customize data label positions of individual data points on an xlsxwriter graph but can only work out how to set data label positions for entire series.

    Simplified reproducible example:

    import xlsxwriter

    workbook = xlsxwriter.Workbook("chart_line.xlsx")
    worksheet = workbook.add_worksheet()

    headings = ["Number", "Batch 1"]
    data = [
    [2, 3, 4, 5],
    [10, 40, 50, 20],
    ]

    worksheet.write_row("A1", headings)
    worksheet.write_column("A2", data[0])
    worksheet.write_column("B2", data[1])

    chart1 = workbook.add_chart({"type": "line"})

    custom_labels = [
    {"value": True},
    {"value": True, "position": "right"},
    {"value": True, "position": "left"}, # where we want to set positions individually
    {"value": True, "position": "above"}, # these "position" keys appear to be ignored
    ]

    chart1.add_series(
    {
    "name": "=Sheet1!$B$1",
    "categories": "=Sheet1!$A$2:$A$5",
    "values": "=Sheet1!$B$2:$B$5",
    "data_labels": {"value": True, "custom": custom_labels} # we can set positions for all labels here with "position": "left"
    }
    )

    worksheet.insert_chart("D2", chart1)
    workbook.close()


    We want to set individual points programmatically for multiple series (not shown above) for visual problems avoided here for simplicity. Xlsxwriter's docs state "The property elements of the custom lists should be dicts with the following allowable keys/sub-properties: ... value, font, delete", which indicates we're out of luck.

    Is there anything we can do? In a closely related question the Xlsxwriter dev appears to state this isn't possible, but we're wondering if we've misunderstood something or if anything has changed in the five years since.

    Continue reading...

Compartilhe esta Página