Python - Openpyxl setting Number_Format to Excel worksheet cells

6,322 views
Skip to first unread message

Bhavesh Patel

unread,
Sep 2, 2018, 1:41:48 PM9/2/18
to openpyxl-users
Hi, I'm using openpyxl to format cells in excel from 'General' to other builtin formats. Although the script completes and states the format has been changed, when I try and open the excel file, excel pops with and error box, repair or cancel.

If the report option is selected, then another pop up prompts read only or exit. If I select read only Excel crashes.

Is the below the correct way to set number formats to cells using openpyxl?

runfile('/.../test.py', wdir='/...python/...')
Reloaded modules: 

Text
Number
Accounting
Currency

import openpyxl wb = openpyxl.load_workbook(xlfile) ws = wb['Sheet1'] ws['B1'].number_format = 'Text' ws['B2'].number_format = 'Number' ws['B3'].number_format = 'Accounting' ws['B4'].number_format = 'Currency' print (ws.cell(row=1,column=2).number_format) print (ws.cell(row=2,column=2).number_format) print (ws.cell(row=3,column=2).number_format) print (ws.cell(row=4,column=2).number_format) wb.save(xlfile)
Screen Shot 2018-09-02 at 18.39.42.png
Screen Shot 2018-09-02 at 18.40.03.png

Charlie Clark

unread,
Sep 3, 2018, 4:23:34 AM9/3/18
to openpyx...@googlegroups.com
Am .09.2018, 19:41 Uhr, schrieb 'Bhavesh Patel' via openpyxl-users
<openpyx...@googlegroups.com>:

> Hi, I'm using openpyxl to format cells in excel from 'General' to other
> builtin formats. Although the script completes and states the format has
> been changed, when I try and open the excel file, excel pops with and
> error box, repair or cancel.

Looks like you're confusing named styles with number formats. Apart from
"General" number formats always take the form of a format template, such
as mmm-yyyy for dates.

Charlie
--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Kronenstr. 27a
Düsseldorf
D- 40217
Tel: +49-211-600-3657
Mobile: +49-178-782-6226
Reply all
Reply to author
Forward
0 new messages