我想将root的标题值写入excel列A,我的代码:
from openpyxl import Workbook
import os
path = "C:/path_to_folder"
#word = '<option value="1.2.0-b.1" key="#SSPVersion#"/>'
os.chdir(path) #change directroy to application notes folder
titlelist = []
for root, dirs, files in os.walk(path):
title = str(root.split("/")[-1])
titlelist.append(title)
wb = Workbook()
ws = wb.active
r=2
for t in titlelist:
ws.cell(row=r, column = 1).value = str(t)
r += 1
wb.save("row_creation_loop.xlsx")
这不起作用...总是显示错误:
traceback(most recent call last):
ws[column_cell+str(row+2)] = stri(i)
self[key].value = value self._bind_value(value)
value = self.check_string(value)
value = unicode(value, self.encoding)
unicodeDecodeError: 'utf8' codec can't decode byte 0*92 in position 17: invalid start byte
答案 0 :(得分:0)
在这里发表一些想法:这里的代码(这是你的副本没有阅读标题工作得很好):
from openpyxl import Workbook
titlelist = ["title1"]
wb = Workbook()
ws = wb.active
for ind,t in enumerate(titlelist):
ws.cell(row= ind+2, column = 1).value = str(t)
wb.save("row_creation_loop.xlsx")
所以这里的问题是你的标题列表,其中包含无法用utf-8编码的字符。我们需要解决这个问题,可能是通过使用一些解码和编码。
与我们分享该列表。