5

How do I change the font size of a cell? I am using OpenPyXL.

It will not let me send this question without typing the above with perfect grammar and spelling, so the last few words people should know what I mean.

1

4 Answers 4

9

This works for me:

from openpyxl.styles import Font
fontStyle = Font(size = "10")
ws.cell(row = 1, column = 1, value = 'Test Text').font = fontStyle

I'm using openpyxl version: 2.3.2

1
4

openpyxl > 3.0 (2021)

In openpyxl handling styles changes a bit between the version.

The current version (as of writing 3.0.6) handles font object as immutable and has deprecated the font.copy(...) function.

Therefore the only way I figured out only change the font size (and keep the rest as it is) is:

ws.cell(1, 1).font += Font(size=new_font_size-ws.cell(1, 1).font)

Explained

Font is an openpyxl.descriptors.serialisable.Serialisable object for which that add operator is implemented and recommended by the

DeprecationWarning: Call to deprecated function copy (Use copy(obj) or cell.obj = cell.obj + other).

Only Problem is that, using the __add__ operator will add the given font size with the original one. Therefore the original one has to be subtracted.

Full Example with Function

from openpyxl import load_workbook, worksheet, cell
from openpyxl.styles import Font

def change_fontsize(cell: cell, fontsize: float):
    orig_size = cell.font.size
    cell.font += Font(size=fontsize - orig_size)

input_filename = "to_modify.xlsx"
wb = load_workbook(input_filename)
# Get first sheet
sh: worksheet = wb[wb.sheetnames[0]]
change_fontsize(sh.cell(1,1), 5)
wb.save('modified.xlsx')

Stability Note

As this functionality is not really documented in openpyxl I assume that one can't assume that this will be stable. Currently it works only because of the implementation of the default values of Fonts __init__ function, which all evaluate False.

1
  • In openpyxl 3.0.7, cells default size value is given with "cell.font.sz". Had to check for not None before setting the substraction
    – udani
    Commented Jun 21, 2021 at 13:44
1

This code opens a *.xlsx file and edits the style of cell 'A1' so it has font size 20. I newer versions of openpyxl you get an error message if you try to overwrite the font directly.

import openpyxl
from openpyxl.styles import Font

path = 'filename.xlsx'
book = openpyxl.load_workbook(path)
ws, *ows = book
ws['A1'].font = Font(size=20)
book.save(path)

The *ows will place any other sheets then the first inside a list, if there is only one worksheet the list will be empty. (ows=[])

-3

Openpyxl styling can achieve this for you!

cell.style.font.size = "font size goes here as integer"
3
  • That didnt work. I have a worksheet names ws. And I want to make the font in a specific cell bigger. What would the actual line of code for that look like. For example would it be something like ws['A2'].cell.font.size = "5"?
    – griffer98
    Commented Mar 7, 2018 at 16:48
  • Obviously the exact code that I wrote isn't going to work.. it's a general example from docs... I am not going to write the code for you. That's not what Stackoverflow is for.
    – Harrison
    Commented Mar 7, 2018 at 16:49
  • 1
    AttributeError: 'str' object has no attribute 'font', so no.
    – Polv
    Commented Jul 17, 2018 at 0:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.