当我遇到以下异常时,我正在尝试将json文件写入Excel电子表格:
Traceback (most recent call last):
File "get_c_data.py", line 28, in <module>
coutput = wb.sheets['output']
File "C:\Users\USERNAME\AppData\Local\Programs\Python\Python36-32\lib\site-packages\xlwings\main.py", line 78, in __getitem__
return self(key)
File "C:\Users\USERNAME\AppData\Local\Programs\Python\Python36-32\lib\site-packages\xlwings\main.py", line 2814, in __call__
return Sheet(impl=self.impl(name_or_index))
File "C:\Users\USERNAME\AppData\Local\Programs\Python\Python36-32\lib\site-packages\xlwings\_xlwindows.py", line 522, in __call__
return Sheet(xl=self.xl(name_or_index))
File "C:\Users\USERNAME\AppData\Local\Programs\Python\Python36-32\lib\site-packages\xlwings\_xlwindows.py", line 152, in __call__
v = self._inner(*args, **kwargs)
File "C:\Users\USERNAME\AppData\Local\Programs\Python\Python36-32\lib\site-packages\win32com\client\dynamic.py", line 197, in __call__
return self._get_good_object_(self._oleobj_.Invoke(*allArgs),self._olerepr_.defaultDispatchName,None)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147352565), None)
我在其他答案上看到该问题通常是32-64位冲突,但是如果使用xlwings应该可以解决。
但是,我 am 已经使用xlwings,并且不确定如何从此处继续。
我认为与错误相关的那部分代码是这些(我遗漏了可以确定的部分,因为它们可以在此脚本的另一个版本中正常运行):
coutput = wb.sheets['output']
loginid = logintab.range('loginid').value
loginpw = logintab.range('loginpw').value
loginurl = logintab.range('loginurl').value
listid = logintab.range('listid').options(numbers=lambda x: str(int(x))).value
id = logintab.range('id').options(numbers=lambda x: str(int(x))).value
# regular login first, plus exception handling
loginoutcome = requests.get(loginurl,auth=(loginid,loginpw))
if loginoutcome.status_code != 200:
raise Exception('Login attempt failed. See error message:' + loginoutcome.text)
# get data
listurl = loginurl + listid + '/'
response = requests.get(listurl,auth=(loginid,loginpw))
if response.status_code != 200:
raise Exception('Access denied. See error message:' + response.text)
dretrieved = response.json()
for n in range(len(dretrieved)):
id.append(dretrieved[n]["id"])
web_id.append(dretrieved[n]['web_id'])
type.append(dretrieved[n]['type'])
create_time.append(dretrieved[n]['create_time'])
archive_url.append(dretrieved[n]['archive_url'])
output.range((2,1)).options(transpose=True).value = id
output.range((2,2)).options(transpose=True).value = web_id
output.range((2,3)).options(transpose=True).value = type
output.range((2,4)).options(transpose=True).value = create_time
output.range((2,5)).options(transpose=True).value = archive_url
print('Data dumped!')