Home > database >  Openpyxl not modifying cell value
Openpyxl not modifying cell value

Time:10-21

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 = wb.active
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] == ' '
                cell_obj.strip()
            if '\n' in cell_obj:
                cell_obj.replace('\n', '')
                cell_obj.strip('\n')
            if str('\n') in cell_obj:
                cell_obj.replace(str('\n'), '')
            if '\\n' in cell_obj:
                cell_obj.strip('\\n')
        sheet.cell(row=row, column=col).value = cell_obj
wb.save(filename=Filename)

CodePudding user response:

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()
'test'
>>> a
'test\n'
>>> b = a.strip()
>>> b
'test'
  • Related