PyXLL The Python Excel Add-In
  • Product
    • Features
    • Get Started
    • Request Demo
    • Download
  • Pricing
  • Resources
    • Documentation
    • Blog
    • Videos
    • FAQ
    • Learn Python
    • Customer Portal
    • About Us
  • Support
    • Documentation
    • Videos
    • FAQ
    • Contact Us
  • Contact Us
Table of Contents
  • PyXLL Documentation
  • Introduction to PyXLL
  • User Guide
    • Installing PyXLL
    • Configuring PyXLL
    • Worksheet Functions
    • Macro Functions
    • Real Time Data
    • Cell Formatting
      • Formatting Worksheet Functions
      • Pandas DataFrame Formatting
      • Custom Formatters
      • Formatting in Macros Functions
    • Charts and Plotting
    • Custom Task Panes
    • ActiveX Controls
    • Using Pandas in Excel
    • Customizing the Ribbon
    • Context Menu Functions
    • Working with Tables
    • Python as a VBA Replacement
    • Menu Functions
    • Reloading and Rebinding
    • Error Handling
    • Deploying your add-in
    • Workbook Metadata
  • Video Guides and Tutorials
  • API Reference
  • What’s new in PyXLL 5
  • Changelog
Close

Formatting in Macros Functions¶

Formatters can also be used from macro functions, as well as from worksheet functions.

To apply a formatter in a macro function use the formatter option to when setting XLCell.value.

For example, to use the standard DataFrameFormatter when setting a DataFrame to a range from an Excel macro you would do the following:

from pyxll import xl_macro, xl_app, XLCell, DataFrameFormatter
import pandas as pd

@xl_macro
def set_dataframe():
    # Get the current selected cell
    xl = xl_app()
    selection = xl.Selection

    # Get an XLCell instance for the selection
    cell = XLCell.from_range(selection)

    # Create a DataFrame
    df = pd.DataFrame({
        "A": [1, 2, 3],
        "B": [4, 5, 6],
        "C": [0.3, 0.6, 0.9]
    })

    # Construct the formatter to be applied
    formatter = DataFrameFormatter()

    # Set the 'value' on the current cell with the formatter
    # and using the auto-resize option
    cell.options(type="dataframe<index=True>",
                 auto_resize=True,
                 formatter=formatter).value = df

The same method can be used from a menu function or ribbon action.

Formatting a DataFrame from an Excel macro
« Custom Formatters
Charts and Plotting »
  • Home
  • Product
  • Features
  • Documentation
  • Download
  • Pricing
  • Support
  • Documentation
  • Videos
  • FAQ
  • Learn Python
  • Contact Us
  • About
  • About Us
  • Legal
  • Blog
© Copyright PyXLL Ltd