S
S
sunekosuri2021-05-06 15:28:50
Python
sunekosuri, 2021-05-06 15:28:50

How to add a character to an existing string in openpyxl?

Let's say I already have an excel file with a table. How to add a character to each cell table.
In my case, I need a space.

There is such a sketch of the code, but it is not working.

wb = openpyxl.reader.excel.load_workbook(filename="schedule.xlsx", data_only=True)
wb.active = 1
sheet = wb.active

for row in range(11, 47):
    for col in range(4, 6):
        a = srt(sheet[str(row)+ str(col)].value) +str(" ")
        cell = sheet.cell(row = row, column = col)
        cell.value = str(a) 

wb.save('example.xlsx')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-05-06
@sunekosuri

from openpyxl.utils import get_column_letter

for row in range(11, 47):
    for col in range(4, 6):
        col_letter = get_column_letter(col)
        cell = sheet[col_letter+str(row)]
        if cell.value:
            cell.value = str(cell.value) + ' '
        else:
            cell.value = ' '

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question