Openpyxl not modifying cell value


So I am running into an issue where I am trying to clean a bunch of newlines out of cells in excel spreadsheets. My program is recognizing \n when it comes across it but .strip() and .replace(‘\n’, ”) are not working. What am I missing here?

#!/usr/bin/env python3
""" Clean up undesired whitespace in some excel files """

from openpyxl import Workbook, load_workbook

Filename = input("Enter filename:\n")

wb = load_workbook(filename = Filename)
sheet =
max_row = sheet.max_row
max_column = sheet.max_column

for row in range(2, max_row + 1):
    for col in range(1, max_column + 1):
        print("In row: {} col: {}".format(row, col))
        cell_obj = str(sheet.cell(row = row, column = col).value)
        if cell_obj is not None:
            if cell_obj[0] == ' ' or cell_obj[-1] == ' '
            if '\n' in cell_obj:
                cell_obj.replace('\n', '')
            if str('\n') in cell_obj:
                cell_obj.replace(str('\n'), '')
            if '\\n' in cell_obj:
        sheet.cell(row=row, column=col).value = cell_obj


The strip and replace methods return transformed results. They do not act on the original immutable string. You are throwing away the results.

>>> a = 'test\n'
>>> a.strip()
>>> a
>>> b = a.strip()
>>> b

Answered By – Passerby

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published