将Python列表导出到Excel [英] Export a Python List to Excel

查看:3043
本文介绍了将Python列表导出到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Win32COM客户端导出一个列表到excel,我已经导入在标题。
我创建的对象编码如下,但我似乎无法让它导出每个值到电子表格中自己的行。如果我可以得到一个好的指针(除了放弃python !!:D),我会很感激。

  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列表!

xprt = XcelExport()
xprt.excel()


解决方案

由于你似乎喜欢我的答案/评论,这里是一个答案:



Python Excel 只是你需要的一切。如果您想要更加集成但似乎有限的功能,则可以使用 IronSpread 。 XLRD和XLWT是伟大的包,但他们不支持* .xlsx文件。 IronSpread仅Windows,并且只支持'07和'10版本的Excel。每个都有它的警告。最后,你可以使用两个(编辑为* .xlsx,然后保存为* .xls(我有一个人有大的* .xls文件的速度问题,但我的脚本写了200mb的文本从那个东西在1分钟。))。



哦,我一定会阅读(skim)文档的有趣的功能,如获取xlrd / xlwt的单元格类型等。



xlwt的简单例子:


$ b这是一个很好的例子, $ b

  import xlwt 
从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 b book.save(TemporaryFile())

xlrd的简单示例:



> import xlrd
from xlrd import open_workbook
book = open_workbook('random.xls')
sheet1 = book。 sheet_by_index(0)
data = []

for x in xrange(sheet1.nrows):
data.append(sheet1.cell(i,1).value)


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)

这篇关于将Python列表导出到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆