Search code examples
pythonopenpyxlunicode-escapes

Python Error Interfacing with excel Writing a formula to a range of cells


I am getting an error associated with this code of unicodeescape codec can't decode bytes in position 2-3; truncated \uxxxxxxxxxxxxxx escape. I'm a new "programmer." Please help. I appreciate it very much!

import os
import openpyxl
wb=openpyxl.load_workbook(os.path.abspath('C:/Users/S/Desktop/Database.xlsx'))
sheet=wb.get_sheet_by_name('Search_Result 2')
for i in range(11503,16897):
    cellref=sheet.cell(row=i,column=4)
    cellref.value="CIQRANGEA(B"&i&",'IQ_COMPANY_ID_QUICK_MATCH',1,1,,,,,'QUICK MATCH COMPANY ID'")
    time.sleep(2)

Solution

  • The Unicode error is due to the \ character in the file path, which initiates an escape sequence. To fix this issue, add an r before the string to use a raw string, like this:

    wb=openpyxl.load_workbook(os.path.abspath(r"C:\Users\S\Desktop\Database.xlsx"))