xlwt模块写入Excel文件时,每个单元格的字符串长度不能超过32767个字符,如果超过了这个限制就会出现“Exception: String longer than 32767 characters”的错误。要解决这个问题,可以将长字符串分割成多个小块,然后分别写入到不同的单元格中。在代码中,可以使用切片的方法将长字符串分割成多个小块,然后使用循环将每个小块写入到不同的单元格中。最后,保存Excel文件即可。

This error occurs because xlwt has a string limit of 32,767 characters per cell. To resolve this error, you can split the string into smaller chunks and write them to separate cells. Here’s an example:

import xlwt

# Example long string
long_string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."

# Create workbook and sheet
workbook = xlwt.Workbook()
sheet = workbook.add_sheet("Sheet1")

# Split the long string into chunks of 32,767 characters and write to separate cells
chunks = [long_string[i:i+32767] for i in range(0, len(long_string), 32767)]
row = 0
for chunk in chunks:
    sheet.write(row, 0, chunk)
    row += 1

# Save workbook
workbook.save("example.xls")

In this example, the long string is split into smaller chunks of 32,767 characters and written to separate cells on a single column.



微信扫描下方的二维码阅读本文

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注