openpyxl convert CSV to EXCEL

后端 未结 3 1145
清酒与你
清酒与你 2020-12-14 12:54

How can I convert a CSV file with : delimiter to XLS (Excel sheet) using openpyxl module?

3条回答
  •  时光取名叫无心
    2020-12-14 13:52

    Here is Adam's solution expanded to strip out characters that openpyxl considers illegal and will throw an exception for:

    import re
    from openpyxl.cell.cell import ILLEGAL_CHARACTERS_RE
    ...
    ##ws.append(row) - Replace with the code below
    for i in row:
        ws.append([ILLEGAL_CHARACTERS_RE.sub('',i)])
    

    ILLEGAL_CHARACTERS_RE is a compiled regular expression containing the characters openpyxl deems "illegal". The code is simply substituting those characters with an empty string.

    Source: Bitbucket openpyxl issue #873 - Remove illegal characters instead of throwing an exception

提交回复
热议问题