如何使用win32com模块在excel中填充列表

时间:2017-03-06 04:27:28

标签: python excel win32com

我正在尝试将列表写入excel,该列表包含几列。

我在下面提供了我的代码。它似乎只提取第一个值。我对python很新,我缺少什么?

代码:

import win32com.client as win32
#
Z = [3,4,6,8,9,11,40]
Q = ['x','y','z','e','g','AA','BB']

excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Add()
ws = wb.Worksheets.Add()
ws.Name = "MyNewSheet"
ws.Range(ws.Cells(1,1),ws.Cells(1,2)).Value = ['Z','Q']
for i,e in enumerate (Z):
ws.Range("A2:A8").Value = [ i for i in (Z)]
for i,e in enumerate (Z):
ws.Range("B2:B8").Value = [ i for i in (Q)]
wb.SaveAs('beta.xlsx')
excel.Application.Quit()

1 个答案:

答案 0 :(得分:2)

它似乎想要将值写为行。因此,这会将写入重新排列为行:

<强>代码:

GET_DATA(.argc = 2, .cmd = SET_SPEED_LIMIT,
         .arg1 = 30, .arg2 = status,
         .dest = &status /*, .limit = 1 - by default */);

<强>结果:

import win32com.client as win32

Z = [3, 4, 6, 8, 9, 11, 40]
Q = ['x', 'y', 'z', 'e', 'g', 'AA', 'BB']

excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Add()
ws = wb.Worksheets.Add()
ws.Name = "MyNewSheet"
ws.Range("A1:B1").Value = ['Z', 'Q']
ws.Range("A2:B8").Value = list(zip(Z, Q))
wb.SaveAs('beta.xlsx')
excel.Application.Quit()