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:

I'm trying to add a newline character in a cell using xlsxwriter. Can somebody please give me some ideas?
worksheet.write_string('A1', "New\nLine") gives me "New\nLine" within the cell A1. I want it to look like:
New
Line
Thank you!
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:

Replace \n with CHAR(10) as per here: https://exceljet.net/formula/add-a-line-break-with-a-formula
python - Adding items from list to a cell separated by new line - Stack Overflow
Newline characters in cells of written workbooks
Excel Writer: Line break in cell
How to enter a newline in the cell
Try adding
'text_wrap':'true'
to the add_format using the first method you describe.
If this also does not work you can try using set_text_wrap() method: http://xlsxwriter.readthedocs.org/en/latest/format.html#set_text_wrap
This will make sure text wraps at newline character '\n'
import xlsxwriter
workbook = xlsxwriter.Workbook('hello.xlsx')
worksheet = workbook.add_worksheet()
header2 = workbook.add_format({
'bold': True,
'align': 'center',
'border': 6,
'valign': 'vcenter',
'fg_color': '#D7E4BC',
'font_name':'Calibri',
'font_size': 12
})
worksheet.merge_range('B4:F6', "CompanyName:ABC \n Country:India", header2)
workbook.close()
It works just fine for me. Maybe you should try the latest XlsxWriter package.
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
workbook = xlsxwriter.Workbook('wrap.xlsx')
worksheet = workbook.add_worksheet()
wrap_format = workbook.add_format({'text_wrap': True})
values = ['1', '2', '3']
# Set an explicit row height for the wrapped text, if required.
#worksheet.set_row(1, 45)
worksheet.write(1, 1, '\n'.join(values), wrap_format)
workbook.close()
Output:

Note, when you wrap text in cells like this Excel normally adjusts the row height automatically to compensate unless you have already manually set the row height. I've put a commented out line in the example to show how to set the row height explicitly.
P.S. \r isn't required.
Probably because of the EOF character, which can be expected to be \r, or \r\f, or \r\n:
sheet.write(1, 11, '\r\n'.join(values), style2)
It depends of the system, and the tool you use to visualize the file.