1

I am trying to write an XLSX file using the openpyxl module. I am using the append() method to insert the values row by row. But I want to insert the values cell by cell instead. I would also like to format the cell (font, colour, alignment etc). Please help me.

Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
  • 4
    Please explain things that you need to do, step by step. Also show what code you've written so far. – shahkalpesh Apr 29 '11 at 07:51
  • The answer of this question [http://stackoverflow.com/questions/8440284/setting-styles-in-openpyxl] has everything you need regarding cell formatting. – mike Aug 01 '13 at 09:19

1 Answers1

6

You can set all that cell by cell (if that is what you need) using openpyxl. Here is a small example which sets the value, name (named_range) and some style of cells in a workbook:

from openpyxl.workbook import Workbook
wb = Workbook()
dest_filename = r'empty_book.xlsx'
ws = wb.worksheets[0]    

name = 'name'

for col_idx in xrange(1, 101):
    col = get_column_letter(col_idx)
    for row in xrange(1, 1001):
        ws.cell('%s%s'%(col, row)).value = '%s%s' % (col, row)
        ws.cell('%s%s'%(col, row)).style.fill.fill_type = 'solid'
        ws.cell('%s%s'%(col, row)).style.fill.start_color.index = openpyxl.style.Color.DARKYELLOW
        wb.create_named_range(name+str(i), ws, '%s%s'%(col, row))

wb.save(filename = dest_filename)

Look up the Style class in the openpyxl documentation to learn more about how to set individual formattings.

Aron Kisdi
  • 544
  • 4
  • 8