Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set cell format and style using Optimized Writer in openpyxl

I've to write a huge Excel file and the optimized writer in openpyxl is what I need.

The question is: is it possibile to set style and format of cells when using optimized writer? Style is not so important (I would only like to highlight column headers), but I need the correct number format for some columns containing currency values. I saw that ws.cell() method is not available when using optimized writer, so how to do it?

Thank you in advance for your help!

like image 527
Augusto Destrero Avatar asked May 17 '13 11:05

Augusto Destrero


People also ask

How do I write to a cell in openpyxl?

Openpyxl write to a cell There are two basic ways to write to a cell: using a key of a worksheet such as A1 or D3, or using a row and column notation with the cell method. In the example, we write two values to two cells. Here, we assing a numerical value to the A1 cell.


1 Answers

As I can't comment, I'll post an update to Dean's answer here:

openpyxl's api (version 2.4.7) has changed slightly so that it should now read:

from openpyxl import Workbook
wb = Workbook( write_only = True )
ws = wb.create_sheet()

from openpyxl.writer.dump_worksheet import WriteOnlyCell
from openpyxl.styles import Font

cell = WriteOnlyCell(ws, value="highlight")
cell.font = Font(name='Courier', size=36)

cols=[]
cols.append(cell)
cols.append("some other value")
ws.append(cols)
wb.save("test.xlsx")

Hope it helps

like image 141
Bob Morane Avatar answered Oct 04 '22 01:10

Bob Morane