If you use xlsxwriter as the Excel writing engine you can add a text wrap format to the column like this:

import pandas as pd

df = pd.DataFrame({'ID': ['111', '222', '222'],
                   'Data': ['Population\nDensity',
                            'Population\nDensity',
                            'Population\nDensity\nArea']})

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_test.xlsx', engine='xlsxwriter')

# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1', index=False)

# Get the xlsxwriter workbook and worksheet objects.
workbook  = writer.book
worksheet = writer.sheets['Sheet1']

# Add a text wrap format.
text_wrap_format = workbook.add_format({'text_wrap': True})

# Add the format to column 2 (zero-indexed) and adjust the width.
worksheet.set_column(1, 1, 15, text_wrap_format)

# Close the Pandas Excel writer and output the Excel file.
writer.close()

Output:

Answer from jmcnamara on Stack Overflow
Discussions

python - Adding items from list to a cell separated by new line - Stack Overflow
I'm trying to do it like this: sheet.write(1, 11, '\n'.join(values), style2) but it seems not to work, because items are added in one line, so it looks like: 123 ... It should work using a \n newline in the string if you also specify text_wrap in the cell format (see the docs): import xlsxwriter ... More on stackoverflow.com
🌐 stackoverflow.com
Newline characters in cells of written workbooks
When values v of cells contain unix line endings \n they do not become line breaks in the corresponding cells of the written workbook. However, when the Windows carriage return newline combos \r\n ... More on github.com
🌐 github.com
20
September 11, 2014
Excel Writer: Line break in cell
How can I write a line break in an excel cell? \r or \n or \r\n all do not work. Context: The different lines are grouped values that should be displayed in the same excel row More on forum.knime.com
🌐 forum.knime.com
2
0
January 9, 2024
How to enter a newline in the cell
How to enter a newline in the cell?"\n" or "\r\n" or "",I tried, but I failed. (ps:I am Chinese, English is not very good, I hope you can understand it.Thanks. ) More on github.com
🌐 github.com
9
November 25, 2015
🌐
XlsxWriter
xlsxwriter.readthedocs.io › format.html
The Format Class - XlsxWriter - Read the Docs
Each unique cell format in an XlsxWriter spreadsheet must have a corresponding Format object. It isn’t possible to use a Format with a write() method and then redefine it for use at a later stage. This is because a Format is applied to a cell not in its current state but in its final state.
🌐
GitHub
github.com › SheetJS › sheetjs › issues › 108
Newline characters in cells of written workbooks · Issue #108 · SheetJS/sheetjs
September 11, 2014 - Windows line endings \r\n only become newlines in Excel when the cell is edited.
Author: SheetJS
🌐
XlsxWriter
xlsxwriter.readthedocs.io › worksheet.html
The Worksheet Class — XlsxWriter
Excel generally merges and centers cells at same time. To get similar behavior with XlsxWriter you need to apply a Format:
🌐
KNIME Community
forum.knime.com › knime analytics platform
Excel Writer: Line break in cell - KNIME Analytics Platform - KNIME Forum Archive
January 9, 2024 - How can I write a line break in an excel cell? \r or \n or \r\n all do not work. Context: The different lines are grouped values that should be displayed in the same excel row
Find elsewhere
🌐
Google Groups
groups.google.com › g › python-excel › c › YVvlIchUXsY
writing a line break into an excel-cell
> So, short answer to your question: use a style (aka XF) with the > Alignment wrap set to 1. > > import xlwt > algn1 = xlwt.Alignment() > algn1.wrap = 1 > style1 = xlwt.XFStyle() > style1.alignment = algn1 > wb = xlwt.Workbook() > ws = wb.add_sheet('LineBreakDemo') > ws.write(10, 0, 'Hard\nLine\nBreak', style1) > ws.write(10, 1, 'Bad\nLine\nBreak') > ws.write(10, 2, 'Soft Line Break', style1) > ws.write(10, 3, 'No Line Break') > wb.save('LineBreakDemo.xls') Thank you very much for explanation and example. This works. All my trials of "\n\r", "\r", "\n\r\n\r" etc. where in vain :) best wishes, Harald
🌐
XlsxWriter
xlsxwriter.readthedocs.io › working_with_textboxes.html
Working with Textboxes — XlsxWriter
Don’t move or size with cells. See Working with Object Positioning for more detailed information about the positioning and scaling of images within a worksheet. The following formatting properties can be set for textbox objects: line border fill gradient font align text_rotation
🌐
GitHub
github.com › tealeg › xlsx › issues › 182
How to enter a newline in the cell · Issue #182 · tealeg/xlsx
November 25, 2015 - How to enter a newline in the cell?"\n" or "\r\n" or " ",I tried, but I failed. (ps:I am Chinese, English is not very good, I hope you can understand it.Thanks.
Author: tealeg
🌐
XlsxWriter
xlsxwriter.readthedocs.io › working_with_data.html
Working with and Writing Data — XlsxWriter
The best way to deal with dataframes or complex data structure is to use Python Pandas. Pandas is a Python data analysis library. It can read, filter and re-arrange small and large data sets and output them in a range of formats including Excel. To use XlsxWriter with Pandas you specify it as the Excel writer engine:
🌐
TutorialsPoint
tutorialspoint.com › python_xlsxwriter › python_xlsxwriter_cell_comments.htm
Python XlsxWriter - Cell Comments
Hence, it cannot be seen until the cursor is placed in the cell. import xlsxwriter wb = xlsxwriter.Workbook('hello.xlsx') ws = wb.add_worksheet() ws.show_comments() data='Python' ws.set_column('C:C', 25) ws.set_row(0, 50) ws.write('C1', data) text = 'Programming language developed by Guido Van Rossum' ws.write_comment('C1', text) data= 'XlsxWriter' ws.set_row(2, 50) ws.write('C3', data) text = 'Developed by John McNamara' ws.write_comment('C3', text, {'visible':False}) data= 'OpenPyXl' ws.set_row(4, 50) ws.write('C5', data) text = 'Developed by Eric Gazoni and Charlie Clark' ws.write_comment('C5', text, {'visible':True}) wb.close()
🌐
Google Groups
groups.google.com › g › python-excel › c › 9eMr2npsKXY › discussion
Trying to write a cell with multiple lines in it
I also notice that if I attempt to edit the cell, it automatically wraps it and the glyph goes away. Perhaps xlwt could similarly perform this transformation automagically? Automagically: each sheet.write(rowx, colx, payload, style) (or equivalent Row method) with a text payload would need to execute code like this (OTTOMH, untested): if '\n' in payload and not style.alignment.wrap: style = copy.deepcopy(style) style.alignment.wrap = 1 Alternatively: Putting hard linebreaks in a cell is usually accompanied by setting horizontal and/or vertical alignment.
🌐
Syncfusion
help.syncfusion.com › documentation › document processing › excel › excel library › net › faqs › how to set a line break inside a cell
How to set a line break inside a cell | XlsIO | Syncfusion
June 7, 2026 - In order to set a line break inside a cell, you have to enable Text Wrapping for the cell, and then break the text. The following code snippet illustrates this. ... using (ExcelEngine excelEngine = new ExcelEngine()) { IApplication application ...
🌐
Google Groups
groups.google.com › g › python-excel › c › __CrBJ6qgqM
New line in cells.
I'm writing cells to an Excel file using the following program (simplified for this posting): --------------------- import xlwt def excel_add_row(sheet, row_number, values): for column, value in enumerate(values): sheet.write(row_number, column, value) def excel_save(filename, *args): workbook=xlwt.Workbook("UTF-8") sheet=workbook.add_sheet("sheet 1") for n, values in enumerate(args): excel_add_row(sheet, n, values) workbook.save(filename) rows=[] for row in range(0, 5): rows.append(["row %d\ncolumn %d" % (row, col) for col in range (0, 5)]) excel_save("test.xls", *rows) --------------------- The problem arises when I open the resulting file, because all newline characters are displayed as a black box. When I double-click a cell the newline is expanded and I have two lines (f.e.
🌐
ColinPaice
colinpaice.blog › category › xlsxwriter
xlsxWriter – ColinPaice
January 31, 2023 - import csv fn = "HF.csv" with open(fn, newline='') as csvfile: reader = csv.DictReader(csvfile) for row in reader: # get the column lables keys = row.keys() ... This opens the specified file chart_scatter.xlsx, for output, and overwrites any previous data. import xlsxwriter ...
🌐
GitHub
github.com › exceljs › exceljs › issues › 353
New line in richText cell · Issue #353 · exceljs/exceljs
July 3, 2017 - By simply adding a character after the \n the line break does not get removed.
Author: exceljs
🌐
GitHub
github.com › mk-j › PHP_XLSXWriter › issues › 114
Enable line wrap and create using formula · Issue #114 · mk-j/PHP_XLSXWriter
May 19, 2017 - Is it possible to enable line wrap within a column from source? i want to write a string like ="ABC"&CHAR(10)&"DEF" but i always the formular itself instead of the line ...
Author: mk-j
🌐
Intellipaat
intellipaat.com › community › 19850 › writing-multi-line-strings-into-cells-using-openpyxl
Writing multi-line strings into cells using openpyxl - Intellipaat Community
August 31, 2019 - I'm trying to write data into a cell, which has multiple line breaks (I believe \n), the ... removed. Is there a way to keep these line breaks?