Wrap Text in Excel using Python

This tutorial guides how to wrap text in Excel using Python. This article shares the details of setting the development environment, provides a list of steps for writing the application, and a running sample demonstrating how to do word wrap in Excel using Python. You will also learn to wrap text in the entire row or column.

Steps for Apply Text Wrapping in Excel using Python

  1. Set the IDE to use Aspose.Cells for Python via Java via Java to auto-wrap text
  2. Create a workbook, access a sheet and fill sample text into it
  3. Get the style object from the target cell whose text you want to wrap
  4. Call the setTextWrapped() method and pass the argument True to set the flag
  5. Set this style object into the target cell again to set this property
  6. Call the autoFitRows() method to wrap the text for cells having the above flag true
  7. Save the resultant file

These steps summarize how to wrap around text in Excel using Python. The main task in this process is to access the style of the target cell, set its text wrap property and save the style back to the cell. Subsequently, when autoFitRows() method is called for the entire sheet, it causes the text wrapping for all the cells whose text wrap property is set.

Code to Auto Wrap Text in Excel using Python

This sample code demonstrates how to fit text in Excel cell using Python. We have used the cell style to set this property, however if you want to get and set this property for an entire row say row 6, then use the expression ws.getCells().getRows().get(5).getStyle() to get the 6th row’s style in the worksheet named ‘ws’. Apply the same process to columns using the getColumns() method instead of the getRows() method in the given expression.

This article has taught us how to apply text wrapping in Excel using Python. If you want to adjust row height to a specific value, refer to the article on how to adjust row height in Excel using Python.