I want to use OpenPyXL to replace the strings in the xlsx file together under multiple conditions.

Asked 2 years ago, Updated 2 years ago, 134 views

I would like to replace it to eliminate half-width and full-width hyphens from strings such as "a-i-u" in xlsx.
I get an error when I put in multiple conditions as shown below, but is there a way to replace them all together under multiple conditions?

wb=openpyxl.load_workbook('book.xlsx')
ws = wb.worksheets [0]

ifos.listdir('Folder with Files'):
    for i in range (ws.max_row):
        for row in ws.iter_rows():
                for cell in row:
           #6 is the column where the string you want to replace exists.
                    ifcell.col_idx==6:
             # substituted portion
                        text=cell.value.replace(["-", "-", "")
                        cell.value=text

·Error code

text=cell.value.replace(["-", "-"], "")
TypeError: replace() argument 1 must be str, not list

If the replacement part can be divided into multiple parts such as text2, then there is no problem with that method.

python python3 openpyxl

2022-09-30 19:12

2 Answers

Wouldn't it be better to replace (delete) the re module?
I want to delete multiple strings in Python -

using re.sub

import re and you will be able to do this

text=re.sub(r"[--]", ", cell.value)
cell.value=text


2022-09-30 19:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.