用pywin32编写阵列以表现出色

发布于 2025-01-22 08:11:44 字数 993 浏览 1 评论 0原文

我试图将阵列写入Excel。代码片段在下面。

导入win32com.client as win32 导入系统 导入numpy作为NP #------------------- 如果 name ==“ main ”:

name="nlve9.xlsm"
excel=win32.Dispatch('Excel.Application')
sheet=excel.Workbooks(name).Worksheets('NLVE')
#--------------
testress=np.zeros(5000)
for i in range(0,5000):
    testress[i]=float(i)

sheet.Range("AC18:AC5016").value=testress
#excel.ScreenUpdating = False
#for i in range(18,5017):
#    sheet.Range("AC" + str(i)).value=testress[i-18]
#excel.ScreenUpdating = True
sys.exit()

When this runs, I get a column of zero the length of testress.  When I replace the last line with below it works but it is excruciatingly slow.  THs is part of an optimization problem so this will run hundreds of times. Hence, I need this to be fast.

for i in range(18,5017):
    #    sheet.Range("AC" + str(i)).value=testress[i-18]

我在第一个方法中做错了什么(sheet.range(“ ac18:ac5016”)。值= testress)?

I am trying to write an array into Excel. The code snippet is below.

import win32com.client as win32
import sys
import numpy as np
#--------------
if name=="main":

name="nlve9.xlsm"
excel=win32.Dispatch('Excel.Application')
sheet=excel.Workbooks(name).Worksheets('NLVE')
#--------------
testress=np.zeros(5000)
for i in range(0,5000):
    testress[i]=float(i)

sheet.Range("AC18:AC5016").value=testress
#excel.ScreenUpdating = False
#for i in range(18,5017):
#    sheet.Range("AC" + str(i)).value=testress[i-18]
#excel.ScreenUpdating = True
sys.exit()

When this runs, I get a column of zero the length of testress.  When I replace the last line with below it works but it is excruciatingly slow.  THs is part of an optimization problem so this will run hundreds of times. Hence, I need this to be fast.

for i in range(18,5017):
    #    sheet.Range("AC" + str(i)).value=testress[i-18]

What am I doing wrong with the first method(sheet.Range("AC18:AC5016").value=testress)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌路黄昏 2025-01-29 08:11:44

如果您使用range.value属性来设置数组,则Excel需要2D阵列的行&列值,即使您的数据是1D数组。

[[R1C1,R1C2,...],[R2C1,R2C2,...] ...]

作为示例:

import win32com.client as wc

xl = wc.gencache.EnsureDispatch('Excel.Application')
xl.Visible = True

wb = xl.Workbooks.Add()
sh = wb.Sheets[1]

sh.Range('A1:A10').Value = [[i] for i in range(10)]

屈服:

net/v3nun.jpg“ rel =“ nofollow noreferrer

”代码,更改:

testress=np.zeros(5000)
for i in range(0,5000):
    testress[i]=float(i)

sheet.Range("AC18:AC5016").value=testress

到:

rowCount = 5000
testress = [[i] for i in range(rowCount)]
sheet.Range('AC18:AC'+str(18+rowCount-1)).Value = testress

If you are using the Range.Value property to set an array, Excel needs a 2D array of row & column values, even if your data is a 1D array.

[[r1c1,r1c2,...],[r2c1,r2c2,...] ...]

As an example:

import win32com.client as wc

xl = wc.gencache.EnsureDispatch('Excel.Application')
xl.Visible = True

wb = xl.Workbooks.Add()
sh = wb.Sheets[1]

sh.Range('A1:A10').Value = [[i] for i in range(10)]

yielding:

enter image description here

EDIT:

From the OP's code, change:

testress=np.zeros(5000)
for i in range(0,5000):
    testress[i]=float(i)

sheet.Range("AC18:AC5016").value=testress

to:

rowCount = 5000
testress = [[i] for i in range(rowCount)]
sheet.Range('AC18:AC'+str(18+rowCount-1)).Value = testress
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文