Jakir Hosen Khan
Jakir Hosen Khan

Reputation: 1528

how to bold csv data in excel?

I work on a python(django) project. I write csv code as follows,

response = HttpResponse(mimetype='text/csv')    
response['Content-Disposition'] = 'attachment; filename=DueDateWiseSearch.csv'    
writer =  csv.writer(response)    
writer.writerow(['Infant Name','Mother Name','Mother Address',
     'Next Vaccine Dose','Due date','Coments'])

this row is the header and I need to bold all header text. I download csv as ms "Excel" file.

How can I do it? Please help!

Upvotes: 14

Views: 50286

Answers (4)

chmullig
chmullig

Reputation: 13406

A more modern alternative is xlsxcessive for exporting an Excel 2007+ .xlsx file. Here's an example that makes the header row slightly larger and bold. It's really not too hard, and documented pretty well.

from xlsxcessive.xlsx import Workbook, save

#Create some fake test data
import urllib, random
words = [x.strip().title() for x in urllib.urlopen('http://dictionary-thesaurus.com/wordlists/Nouns%285,449%29.txt').readlines()]
names = []
for i in range(25):
    address = '%s %s %s' % (random.randint(0, 100000), random.choice(words), random.choice(['Ave', 'St', 'Blvd', 'Ct', 'Ln']))
    names.append((random.choice(words), random.choice(words), address))

#Create the workbook and sheet
wb = Workbook()
s1 = wb.new_sheet('Sheet 1')

#Create the bold style for the header row
headerfmt = wb.stylesheet.new_format()
headerfmt.font(size=14, bold=True)

#Write out the header row
header = ['Infant Name','Mother Name','Mother Address',]
for col, label in enumerate(header):
    s1.cell(coords=(0,col), value=label, format=headerfmt)

#Write out the data    
for rownumber, rowvalues in enumerate(names, start=1):
    for col, value in enumerate(rowvalues):
        s1.cell(coords=(rownumber, col), value=value)

save(wb, 'stackoverflow_problem.xlsx')

Upvotes: 1

Tauren
Tauren

Reputation: 27235

CSV only contains data, it doesn't contain any formatting information. If you need to format your data, I'd suggest actually creating an XLS file instead of a CSV file. Try using something like xlwt.

Upvotes: 6

mkoistinen
mkoistinen

Reputation: 7773

There is no way to do that with CSV that I know of, but you might consider using the old SYLK format or Office XML format.

Upvotes: 1

SingleNegationElimination
SingleNegationElimination

Reputation: 156158

There's no way to do that in CSV. You could all caps the output, or you could use another format that supports text styles.

Upvotes: 24

Related Questions