我想用xlwings向Excel写一个Array(40行,2列),但是不能用。经过大量尝试,我发现当数组的行大于36时,它无法写入Excel。
代码如下:
import xlwings as xw
data = [['20180714', 'B8A156WH4T601'], ['20180714', 'B8A156WH4T601'], ['20180714', 'B8A156WH4T601'], ['20180714', 'B8A156WH4T601'], ['20180714', 'B8A156WH4T601'], ['20180714', 'B8A156WH4T601'], ['20180715', 'B8A156WH4T601'], ['20180715', 'B8A156WH4T601'], ['20180715', 'B8A156WH4T601'], ['20180715', 'B8A156WH4T601'], ['20180715', 'B8A156WH4T601'], ['20180715', 'B8A156WH4T601'], ['20180715', 'B8A320WH5V405'], ['20180715', 'B8A320WH5V405'], ['20180715', 'B8A320WH5V405'], ['20180715', 'B8A320WH5V405'], ['20180715', 'B8A320WH5V405'], ['20180715', 'B8A320WH5V405'], ['20180715', 'B8A490QU5V504'], ['20180715', 'B8A490QU5V504'], [
'20180715', 'B8A490QU5V504'], ['20180715', 'B8A490QU5V504'], ['20180715', 'B8A490QU5V504'], ['20180715', 'B8A490QU5V504'], ['20180716', 'B8A156FH4V601'], ['20180716', 'B8A156FH4V601'], ['20180716', 'B8A156FH4V601'], ['20180716', 'B8A156FH4V601'], ['20180716', 'B8A156FH4V601'], ['20180716', 'B8A156FH4V601'], ['20180716', 'B8A156FH4V601'], ['20180716', 'B8A156FH4V601'], ['20180716', 'B8A156FH4V601'], ['20180716', 'B8A156FH4V601'], ['20180716', 'B8A156FH4V601'], ['20180716', 'B8A156FH4V601'], ['20180716'], ['20180716', 'B8A156FH4V601'], ['20180716', 'B8A156FH4V601'], ['20180716', 'B8A156FH4V601']]
print(len(data))
app = xw.App(visible=False, add_book=False)
wb = app.books.add()
sht = wb.sheets["sheet1"]
sht.range("A2").value = data[0:37]
print(sht.range("A2").value)
wb.save("CS_PT_By_Tray.xlsx")
wb.close()
app.quit()
上面的代码无法写入Excel,但是如果我更改
sht.range("A2").value = data[0:37]
到
sht.range("A2").value = data[0:36]
确实有效。
怎么可能呢?
答案 0 :(得分:0)
好吧,您正在遍历数组中的元素,对。我刚刚更改了这一行:
sht.range("A2").value = data[10:20]
我也更改了一些数据点。
data = [['20180714', 'B8A156WH4T601'], ['20180714', 'B8A156WH4T602'], ['20180714', 'B8A156WH4T603'], ['20180714', 'B8A156WH4T604'], ['20180714', 'B8A156WH4T605'], ['20180714', 'B8A156WH4T606'], ['20180715', 'B8A156WH4T607'], ['20180715', 'B8A156WH4T608'], ['20180715', 'B8A156WH4T609'], ['20180715', 'B8A156WH4T6010'], ['20180715', 'B8A156WH4T6011'], ['20180715', 'B8A156WH4T6012'], ['20180715', 'B8A320WH5V4013'], ['20180715', 'B8A320WH5V4014'], ['20180715', 'B8A320WH5V4015'], ['20180715', 'B8A320WH5V4016'], ['20180715', 'B8A320WH5V4017'], ['20180715', 'B8A320WH5V4018'], ['20180715', 'B8A490QU5V5019'], ['20180715', 'B8A490QU5V5020'], [
'20180715', 'B8A490QU5V504'], ['20180715', 'B8A490QU5V504'], ['20180715', 'B8A490QU5V504'], ['20180715', 'B8A490QU5V504'], ['20180716', 'B8A156FH4V601'], ['20180716', 'B8A156FH4V601'], ['20180716', 'B8A156FH4V601'], ['20180716', 'B8A156FH4V601'], ['20180716', 'B8A156FH4V601'], ['20180716', 'B8A156FH4V601'], ['20180716', 'B8A156FH4V601'], ['20180716', 'B8A156FH4V601'], ['20180716', 'B8A156FH4V601'], ['20180716', 'B8A156FH4V601'], ['20180716', 'B8A156FH4V601'], ['20180716', 'B8A156FH4V601'], ['20180716'], ['20180716', 'B8A156FH4V601'], ['20180716', 'B8A156FH4V601'], ['20180716', 'B8A156FH4V601']]
我将结束号从01更改为20。现在,我运行脚本并得到它。
那不是您所期望的吗?