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

Feature request: Checkbox #1092

Closed
areqq opened this issue Sep 15, 2024 · 5 comments
Closed

Feature request: Checkbox #1092

areqq opened this issue Sep 15, 2024 · 5 comments
Assignees
Labels
awaiting user feedback Waiting for users to answer a question or test a fix. feature request in progress The fix/feature is being worked on.

Comments

@areqq
Copy link

areqq commented Sep 15, 2024

Question

Is it possible to insert checkbox into cell?

@areqq areqq added the question label Sep 15, 2024
@jmcnamara
Copy link
Owner

Which type of checkbox do you mean?

None of them are currently supported but I may be inclined to add the newer Checkbox (see below) but not the form style checkbox.

screenshot 3

@jmcnamara jmcnamara self-assigned this Sep 15, 2024
@areqq
Copy link
Author

areqq commented Sep 15, 2024

yes, this one

@jmcnamara jmcnamara added feature request medium term Probably will be added. and removed question labels Oct 23, 2024
@jmcnamara jmcnamara changed the title question: Checkbox Feature request: Checkbox Oct 23, 2024
jmcnamara added a commit that referenced this issue Jan 26, 2025
jmcnamara added a commit that referenced this issue Jan 26, 2025
@jmcnamara
Copy link
Owner

@areqq and @tjpartridge

I've pushed initial support for this feature to main.

You can now use it as follows:

import xlsxwriter

# Create a new Excel file object.
workbook = xlsxwriter.Workbook("checkbox.xlsx")

# Add a worksheet to the workbook.
worksheet = workbook.add_worksheet()

# Create some formats to use in the worksheet.
bold = workbook.add_format({"bold": True})
light_red = workbook.add_format({"bg_color": "#FFC7CE"})
light_green = workbook.add_format({"bg_color": "#C6EFCE"})

# Set the column width for clarity.
worksheet.set_column(0, 0, 30)

# Write some descriptions.
worksheet.write(1, 0, "Some simple checkboxes:", bold)
worksheet.write(4, 0, "Some checkboxes with cell formats:", bold)

# Insert some boolean checkboxes to the worksheet.
worksheet.insert_checkbox(1, 1, False)
worksheet.insert_checkbox(2, 1, True)

# Insert some checkboxes with cell formats.
worksheet.insert_checkbox(4, 1, False, light_red)
worksheet.insert_checkbox(5, 1, True, light_green)

# Close the workbook.
workbook.close()

Output:

Image

There is still some work to be done to ensure that this feature is compatible with Tables and Table formats. Also, it currently modifies a user supplied format (such as light_ref and light_green in the example above) to add the checkbox format property. This potentially could cause issues if the format is used elsewhere. So I will need to add a more robust solution for that. So overall it is good for testing, if you get a chance, but it still has a few small issues to be fixed.

@jmcnamara jmcnamara added awaiting user feedback Waiting for users to answer a question or test a fix. in progress The fix/feature is being worked on. and removed medium term Probably will be added. labels Jan 27, 2025
jmcnamara added a commit that referenced this issue Jan 27, 2025
jmcnamara added a commit that referenced this issue Jan 27, 2025
jmcnamara added a commit that referenced this issue Jan 27, 2025
jmcnamara added a commit that referenced this issue Jan 28, 2025
jmcnamara added a commit that referenced this issue Jan 28, 2025
@jmcnamara
Copy link
Owner

There is still some work to be done to ensure that this feature is compatible with Tables and Table formats.

I fixed this and added some tests. You can now do something like this using Polars (or with just XlsxWriter):

import polars as pl

# Create a Pandas dataframe with some sample data.
df = pl.DataFrame(
    {
        "Region": ["North", "South", "East", "West"],
        "Target": [100, 70, 90, 120],
        "On-track": [False, True, True, False],
    }
)

# Write the dataframe to a new Excel file with formatting options.
df.write_excel(
    workbook="polars_checkbox.xlsx",

    # Set the checkbox format for the "On-track" boolean column.
    column_formats={"On-track": {"checkbox": True}},

    # Set an alternative table style.
    table_style="Table Style Light 9",

    # Autofit the column widths.
    autofit=True,
)

Output:

Image

Also, it currently modifies a user supplied format (such as light_ref and light_green in the example above) to add the checkbox format property. This potentially could cause issues if the format is used elsewhere. So I will need to add a more robust solution for that.

This is probably going to need a reasonable refactoring of the Format code so I am going to tackle that in the next release.

As such I am going to push the current code out in a new release in the next 12 hours.

@jmcnamara
Copy link
Owner

This feature is available in XlsxWriter v3.2.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting user feedback Waiting for users to answer a question or test a fix. feature request in progress The fix/feature is being worked on.
Projects
None yet
Development

No branches or pull requests

2 participants