Writing multi-line strings into cells using openpyxl

后端 未结 3 771
迷失自我
迷失自我 2020-12-09 08:09

I\'m trying to write data into a cell, which has multiple line breaks (I believe \\n), the resulting .xlsx has line breaks removed. Is there a way to keep these line breaks?

3条回答
  •  醉话见心
    2020-12-09 08:46

    Just an additional option, you can use text blocking """ my cell info here """ along with the text wrap Boolean in alignment and get the desired result as well.

    from openpyxl import Workbook
    
    wb= Workbook()
    sheet= wb.active
    sheet.title = "Sheet1"
    
    sheet['A1'] = """Line 1
    Line 2
    Line 3"""
    
    sheet['A1'].alignment = Alignment(wrapText=True)
    
    wb.save('wrap_text1.xlsx')
    

提交回复
热议问题