0

I want to remove % and replace with a string, and to remove space and replace it with under score.

This is what I have done so far:

# Open Excel file from a user imput
import xlrd, xlwt
filename = raw_input("Enter Excel file name with extension (.xls) and path")
oldbook = xlrd.open_workbook(filename)
newbook = xlwt.Workbook()

# For all the sheets in the workbook
for sheetname in oldbook.sheet_names():
    oldsheet = oldbook.sheet_by_name(sheetname)
    newsheet = newbook.add_sheet(sheetname)

    # For all the rows and all the columns in an excel
    for ii in range(oldsheet.nrows):
        for jj in range(oldsheet.ncols):
            # Replace
            range.replace("%", "Perc")

# Save the file in a desired location with the desired name
savelocation = raw_input("Enter a new path and file name with extension (.xls) to save the new Excel spread sheet ")
newbook.save(savelocation)

1 Answer 1

2

One advice, read cell data into a string and then manipulate it.

Try this: (Unfortunately I cannot run it at the moment)

# Open Excel file from a user imput
import xlrd, xlwt
filename = raw_input("Enter Excel file name with extension (.xls) and path")
oldbook = xlrd.open_workbook(filename)
newbook = xlwt.Workbook()

# For all the sheets in the workbook
for sheetname in oldbook.sheet_names():
    oldsheet = oldbook.sheet_by_name(sheetname)
    newsheet = newbook.add_sheet(sheetname)

    # For all the rows and all the columns in an excel
    for ii in range(oldsheet.nrows):
        for jj in range(oldsheet.ncols):
            # Replace
            CellString=str(oldsheet.cell(ii, jj).Value)
            CellString=CellString.replace("%", "Perc")
            CellString=CellString.replace(" ", "_")
            newsheet.write(ii, jj, CellString)
# Save the file in a desired location with the desired name
savelocation = raw_input("Enter a new path and file name with extension (.xls) to save the new Excel spread sheet ")
newbook.save(savelocation)
Sign up to request clarification or add additional context in comments.

5 Comments

i ran the code as you said and nothing happened. i dont get an error but the values whwere not replaced
Yes i get the spreadsheet with all my numbers formatted as string but they are not changed.... the % are still symbols and empty spaces in the excel are not subbed with underscore.
The replace method does not modify strings in-place. (Actually, Python strings cannot be modified in-place.) Instead, the method returns a new string. So you need to have something to receive the return value (which could be the same name if you like).
True! I don't know what I was thinking yesterday. Thanks a lot, now it is corrected.
i still dont get it. i added another variable to received the replace but no result. how do i put the replaced value back where they belong ?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.