Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(python): Support passing Worksheet objects to the write_excel method #18031

Merged
merged 2 commits into from
Aug 4, 2024

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Aug 3, 2024

Closes #17994.
Closes #17968.

Example

from xlsxwriter import Workbook
import polars as pl

df = pl.DataFrame({
    "id": ["a123", "b345", "c567", "d789", "e101"],
    "points": [99, 45, 50, 85, 35],
})

with Workbook("basic_chart.xlsx") as wb:
    # create worksheet object
    ws = wb.add_worksheet("demo")

    # write polars frame data to the worksheet
    df.write_excel(
        workbook=wb,
        worksheet=ws,
        table_name="DataTable",
        table_style="Table Style Medium 26",
        hide_gridlines=True,
    )

    # create a chart object against the written
    # frame data using structured references
    chart = wb.add_chart({"type": "column"})
    chart.set_title({"name": "Example Chart"})
    chart.set_legend({"none": True})
    chart.set_style(38)
    chart.add_series({
        "values": "=DataTable[points]",
        "categories": "=DataTable[id]",
        "data_labels": {"value": True},
    })

    # add chart to the worksheet
    ws.insert_chart("D1", chart)
write_xl_worksheet

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars labels Aug 3, 2024
@alexander-beedie alexander-beedie added the A-io-excel Area: reading/writing Excel files label Aug 3, 2024
Copy link

codecov bot commented Aug 3, 2024

Codecov Report

Attention: Patch coverage is 84.61538% with 2 lines in your changes missing coverage. Please review.

Project coverage is 80.51%. Comparing base (d5265d3) to head (2b459fc).
Report is 1 commits behind head on main.

Files Patch % Lines
py-polars/polars/io/spreadsheet/_write_utils.py 84.61% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #18031      +/-   ##
==========================================
+ Coverage   80.49%   80.51%   +0.01%     
==========================================
  Files        1496     1496              
  Lines      196786   196797      +11     
  Branches     2817     2820       +3     
==========================================
+ Hits       158407   158445      +38     
+ Misses      37858    37830      -28     
- Partials      521      522       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@ritchie46 ritchie46 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All your PR's with images deserve their own chapter in the user guide. I think we undersell what your write_excel is capable off. :D

@ritchie46 ritchie46 merged commit b1cb91e into pola-rs:main Aug 4, 2024
15 checks passed
@alexander-beedie alexander-beedie deleted the xl-write-worksheet-object branch August 4, 2024 08:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-io-excel Area: reading/writing Excel files enhancement New feature or an improvement of an existing feature python Related to Python Polars
Projects
None yet
2 participants