-1

I want to write root's title value to the excel column A, my code:

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")

This does not work...always shows Error :

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
ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
Jennifer Zou
  • 187
  • 1
  • 1
  • 12
  • Possible duplicate of [UnicodeDecodeError: 'utf8' codec can't decode byte 0xa5 in position 0: invalid start byte](https://stackoverflow.com/questions/22216076/unicodedecodeerror-utf8-codec-cant-decode-byte-0xa5-in-position-0-invalid-s) – chickity china chinese chicken Oct 23 '17 at 23:31
  • DO NOT EDIT TRACEBACKS UNLESS YOU KNOW WHAT YOU'RE DOING. It's useless this way, we do need to know both lines and their locations. – ivan_pozdeev Oct 24 '17 at 00:53

1 Answers1

0

Just posting some thought here: This code here (which is a copy of yours without reading in titles works just fine):

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")

So the issue here is your titlelist which contains characters that can't be encoded in utf-8. We need to fix that, probably by using some decode and encode.

Share that list with us.

Anton vBR
  • 18,287
  • 5
  • 40
  • 46