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

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

问题描述

我正在尝试通过我在标题处导入的 Win32COM 客户端将列表导出到 excel.我创建的对象编码如下,但我似乎无法让它将每个值导出到电子表格中自己的行.如果我能得到一个好的指针(除了放弃 python !!:D),我将不胜感激.

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 拥有您所需要的一切.如果您想要更集成但似乎有限的东西,则有 IronSpread.XLRD 和 XLWT 是很棒的软件包,但它们不支持 *.xlsx 文件.IronSpread 仅适用于 Windows,并且仅支持 '07 和 '10 版本的 Excel.每个都有它的警告.最后,您可以同时使用两者(编辑为 *.xlsx,然后另存为 *.xls(我有人遇到过大 *.xls 文件的速度问题,但我的脚本在 1分钟.)).

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.)).

哦,我肯定会阅读(略读)文档以了解有趣的功能,例如获取 xlrd/xlwt 的细胞类型等.这是值得的,如果只是因为它很短,并且可以节省您进行实验的学习曲线.

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.

xlwt 的超短示例:

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())

xlrd 的超短示例:

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天全站免登陆