我想将一些数据附加到Excel文件中:
import os
from xlutils.copy import copy
import xlrd as ExcelRead
def write_append(file_name):
values = ["Ann", "woman", 22, "UK"]
r_xls = ExcelRead.open_workbook(file_name,formatting_info=True,on_demand=True)
r_sheet = r_xls.sheet_by_index(0)
rows = r_sheet.nrows
w_xls = copy(r_xls)
sheet_write = w_xls.get_sheet(0)
for i in range(0, len(values)):
sheet_write.write(rows, i, values[i])
w_xls.save(file_name)
if __name__ == "__main__":
write_append("./bzxx.xls")
但是得到的错误结果如下:
D:\Python\Environment\env_finance\Scripts\python.exe
E:/Project/Finance/code/financeSystem/temp/copyfile.py
Traceback (most recent call last):
File "E:/Project/Finance/code/financeSystem/temp/copyfile.py", line 36, in <module>
write_append("./bzxx.xls")
File "E:/Project/Finance/code/financeSystem/temp/copyfile.py", line 31, in write_append
w_xls.save(file_name)
File "D:\Python\Environment\env_finance\lib\site-packages\xlwt\Workbook.py", line 696, in save
doc.save(filename_or_stream, self.get_biff_data())
File "D:\Python\Environment\env_finance\lib\site-packages\xlwt\Workbook.py", line 653, in get_biff_data
before += self.__all_fonts_num_formats_xf_styles_rec()
File "D:\Python\Environment\env_finance\lib\site-packages\xlwt\Workbook.py", line 556, in __all_fonts_num_formats_xf_styles_rec
return self.__styles.get_biff_data()
File "D:\Python\Environment\env_finance\lib\site-packages\xlwt\Style.py", line 185, in get_biff_data
result += self._all_num_formats()
File "D:\Python\Environment\env_finance\lib\site-packages\xlwt\Style.py", line 209, in _all_num_formats
result += NumberFormatRecord(fmtidx, fmtstr).get()
File "D:\Python\Environment\env_finance\lib\site-packages\xlwt\BIFFRecords.py", line 785, in __init__
ufmtstr = upack2(fmtstr)
File "D:\Python\Environment\env_finance\lib\site-packages\xlwt\UnicodeUtils.py", line 50, in upack2
us = unicode(s, encoding)
TypeError: coercing to Unicode: need string or buffer, NoneType found
Excel格式化,无法更改。我想为它添加一些数据;你能帮忙吗?