如何使用Pythonwin32.com加速excel文件编写

时间:2012-06-28 11:48:20

标签: python numpy win32com

我正在将ascii文件中的数据读入数组,并将数据写入excel文件以进行后续处理。共有337列和1100行 - 在xlsx中生成370700个单元格。

编写Excel数据文件需要很长时间 - 在16 GB机器上大约需要25-35分钟。

还有其他方法可以更有效地在excel文件中写入数据吗?

任何帮助都会得到满足。

import numpy as np 
import win32com.client as win32 
import matplotlib.pyplot as plt 
from win32com import client  from time
clock, time  start_time = clock()

itr = int(raw_input('Enter number of iteration = '))
f1 =open("Case_AT-6_WF_fl2.gid","r")

s2 = f1.read() 
a = s2.split() 

count=0 for col in open("Case_AT-6_WF_fl2.gid",'r'):
count = count+1 column = int(len(col.split()))
sm = int(itr * column)

digit = map(float,(a[(len(a)-sm):l
a[(column+7):(column*2+7)]

# preparing 2D matrix dig = np.array(digit)  dig1 =(dig.reshape

### *******Plotting
## print name

for i in range(len(name)):    
    print i,"=",name[i]

plot = int(raw_input("\nEnter variable number for plot =" ))

# writing Excel file*******************************************

print('file printing option: 1 = Yes 2= No') q = int(raw_input(
printing option = "))

if q==1 :
    excel = win32.gencache.EnsureDispatch('Excel.Application')
    excel.Visible = False
    wb = excel.Workbooks.Add()
    ws=wb.Sheets(1)
    ws.Name = "MyData"
    ws.Activate
    for kk in range(len(name)):
        ws.Cells(2,kk+1).Value = name[kk]
        ws.Cells(3,kk+1).Value = unit[kk]
m = 0
k =0
for j in range(itr):
    for k in range(column):
        ws.Cells(j+5,k+1).Value=dig1[j][k]
        m = m+1
       print '',m
print clock() - start_time, "seconds"

wb.SaveAs('C:\\Documents andSettings\\sd47086\\Desktop\\test\\MyWorkbook3.xlsx')
excel.Application.Quit()

1 个答案:

答案 0 :(得分:0)

分配范围通常比单独设置每个单元格要快得多。 您只需要确保python序列与范围的大小匹配,例如     ws.Range('A1:B10000')。值=('a','b')* 10000