Xlsx Writer: Writing stings with multiple format in a single cell
<p>I have a string with multiple semicolons.</p>

<pre><code>'firstline: abc \n secondline: bcd \n thirdline: efg'
</code></pre>

<p>I want to write this strings in excel cell using Xlsx Writer like below.</p>

<p><b>firstline</b>: abc <br>
<b>secondline</b>: bcd <br>
<b>thirdline</b>: efg <br></p>

<p>This is what I did.</p>

<pre><code>description_combined = 
    '''
    firstline: abc
    secondline: bcd
    thirdline: efg
    '''
    spanInserted = []
    spanInserted = description_combined.split("\n")
    result = ''

    for spans in spanInserted:
        strr1 =  '{}:'.format(spans.split(":")[0])
        strr2 = spans.split(":")[1]
        result += '''bold , "{}" , "{}" ,'''.format(str(strr1),str(strr2))

    result = result[:-1]
    # print result

    worksheet.write_rich_string('A1', result)  
</code></pre>

<p><br>
This is the result I got in excel cell:</p>

<blockquote>
  <p>bold , "firstline:" , "abc" ,bold , "secondline:" , "bcd" ,bold , "thirdline:" , "efg"</p>
</blockquote>

<p>The jmcnamara's solution works well. I posted a similar possible solution, but dynamic. </p>

<pre><code>import xlsxwriter
workbook = xlsxwriter.Workbook("&lt;your path&gt;")
worksheet = workbook.add_worksheet()
# add style for first column
cell_format = workbook.add_format({'bold': True})
text_wrap = workbook.add_format({'text_wrap': True, 'valign': 'top'})
data = []
description_combined = 'firstline: abc \n secondline: bcd \n thirdline: efg'
# prepare list of list
for item in description_combined.split("\n"):
    data.append(cell_format)
    data.append(item.split(":")[0].strip() + ":")
    data.append(item.split(":")[1] + "\n")

# write data in one single cell
data.append(text_wrap)

worksheet.write_rich_string('A1', *data)

workbook.close()
</code></pre>

<p>I hope that will be helpful</p>

python excel python-2.7 xlsx xlsxwriter