python列表导出_将Python列表导出到Excel

1586010002-jmsa.png

I am trying to export a list to excel via the Win32COM client whihc i have imported at the header.

The object i created is coded as below, but I cant seem to get it to export each value to its own row in the spreadsheet. If I can get a good pointer (other than give up python!! :D), I would appreciate it.

class XcelExport():

def excel(self):

app = 'Excel'

xl = win32.gencache.EnsureDispatch('%s.Application' % app)

ss = xl.Workbooks.Open(r'C:\MyFile.xls')

sh = ss.ActiveSheet

xl.Visible = True

sleep(.1)

sh.Cells(1,1).Value = 'Export-to-%s : Items' % app

sleep(.1)

for i in EventTableRFT:

sh.Range("A").Value = i

sh.Cells(i+2,1).Value = "End of the List!"

xprt = XcelExport()

xprt.excel()

解决方案

Since you seemed to like my answer/comment, here's an answer proper:

Python Excel has just about everything you'd ever need. If you want something more integrated but seems limited, there is IronSpread. XLRD and XLWT are great packages, but they don't support *.xlsx files. IronSpread is Windows only and only support '07 and '10 versions of Excel. Each has it's caveats. In the end, you can use both (edit as *.xlsx, then save as to *.xls (I had someone who had speed issues with large *.xls files, but my script wrote 200mb of text from that thing in like 1 minute.)).

Oh, and I would definitely read (skim) the documentation for interesting features such as getting the cell types etc of xlrd/xlwt. It's worth it, if only because it's short and will save you the learning curve of experimenting.

Super short example of xlwt:

import xlwt

from tempfile import TemporaryFile

book = xlwt.Workbook()

sheet1 = book.add_sheet('sheet1')

supersecretdata = [34,123,4,1234,12,34,12,41,234,123,4,123,1,45123,5,43,61,3,56]

for i,e in enumerate(supersecretdata):

sheet1.write(i,1,e)

name = "random.xls"

book.save(name)

book.save(TemporaryFile())

Super short example of xlrd:

import xlrd

from xlrd import open_workbook

book = open_workbook('random.xls')

sheet1 = book.sheet_by_index(0)

data = []

for i in xrange(sheet1.nrows):

data.append(sheet1.cell(i,1).value)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值