Openpyxl.utils.exceptions.IllegalcharacterError

后端 未结 3 2256
遥遥无期
遥遥无期 2021-01-05 15:19

I have the following python code to write processed words into excel file. The words are about 7729

From openpyxl import *
book=Workbook ()
sheet=book.active
         


        
相关标签:
3条回答
  • 2021-01-05 15:38

    You missed to add the value for cell sheet.cell (row=1,column=x+1).value =

    Try like this

    from openpyxl import *
    book = Workbook ()
    sheet = book.active
    sheet.title = "test"
    for x in range (7):
        sheet.cell (row=1,column=x+1).value = "Hello"
    book.save ('test.xlsx')
    
    0 讨论(0)
  • 2021-01-05 15:44

    I faced similar issue and found out that it is because of \xa1 character which is hex value of ascii 26 (SUB). Openpyxl is not allowing to write such characters (ascii code < 32). I tried xlsxwriter library without any issue it worte this character in xlsx file.

    0 讨论(0)
  • 2021-01-05 15:49

    Try this : This code works for me .

    from openpyxl import *
    book=Workbook ()
    sheet=book.active
    sheet.title="test"
    x = 0
    with open("temp.txt") as myfile :
        text = myfile.readline()
        while text !="":
                sheet.cell (row=1,column=x+1).value=str(text).encode("ascii",errors="ignore")
                x+=1
                text = myfile.readline()
    
    book.save ('test.xlsx')
    
    0 讨论(0)
提交回复
热议问题