最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【记录】Python中生成(写入数据到)Excel文件中

Python crifan 21720浏览 0评论

想要试试,在Python中,如何将数据,导出为Excel,即把数据写入到新生成的excel文件。


1.网上看到:

Working with Excel Files in Python

其中包括,Python中,如何读取excel文件,如何写入数据到excel文件等等相关的库。

看起来应该是这方面的资料中,总结的最好的了.

 

2.此处暂时只需要写入数据到Excel,所以就去参考:

xlwt examples

去看看示例代码:

https://github.com/python-excel/xlwt/blob/master/xlwt/examples/simple.py

貌似用起来还算简单。

然后去主页:

http://pypi.python.org/pypi/xlwt

下载xlwt-0.7.4.tar.gz后,解压。

然后参考:

https://secure.simplistix.co.uk/svn/xlwt/trunk

https://secure.simplistix.co.uk/svn/xlwt/trunk/README.html

去用setup.py install去安装:

D:\tmp\dev_tools\python\excel\xlwt-0.7.4.tar\xlwt-0.7.4\xlwt-0.7.4>setup.py install
running install
running build
running build_py
creating build
creating build\lib
creating build\lib\xlwt
copying xlwt\antlr.py -> build\lib\xlwt
copying xlwt\BIFFRecords.py -> build\lib\xlwt
copying xlwt\Bitmap.py -> build\lib\xlwt
copying xlwt\Cell.py -> build\lib\xlwt
copying xlwt\Column.py -> build\lib\xlwt
copying xlwt\CompoundDoc.py -> build\lib\xlwt
copying xlwt\ExcelFormula.py -> build\lib\xlwt
copying xlwt\ExcelFormulaLexer.py -> build\lib\xlwt
copying xlwt\ExcelFormulaParser.py -> build\lib\xlwt
copying xlwt\ExcelMagic.py -> build\lib\xlwt
copying xlwt\Formatting.py -> build\lib\xlwt
copying xlwt\Row.py -> build\lib\xlwt
copying xlwt\Style.py -> build\lib\xlwt
copying xlwt\UnicodeUtils.py -> build\lib\xlwt
copying xlwt\Utils.py -> build\lib\xlwt
copying xlwt\Workbook.py -> build\lib\xlwt
copying xlwt\Worksheet.py -> build\lib\xlwt
copying xlwt\__init__.py -> build\lib\xlwt
creating build\lib\xlwt\doc
copying xlwt\doc\xlwt.html -> build\lib\xlwt\doc
creating build\lib\xlwt\examples
copying xlwt\examples\big-16Mb.py -> build\lib\xlwt\examples
copying xlwt\examples\big-35Mb.py -> build\lib\xlwt\examples
copying xlwt\examples\blanks.py -> build\lib\xlwt\examples
copying xlwt\examples\col_width.py -> build\lib\xlwt\examples
copying xlwt\examples\country.py -> build\lib\xlwt\examples
copying xlwt\examples\dates.py -> build\lib\xlwt\examples
copying xlwt\examples\format.py -> build\lib\xlwt\examples
copying xlwt\examples\formulas.py -> build\lib\xlwt\examples
copying xlwt\examples\formula_names.py -> build\lib\xlwt\examples
copying xlwt\examples\hyperlinks.py -> build\lib\xlwt\examples
copying xlwt\examples\image.py -> build\lib\xlwt\examples
copying xlwt\examples\merged.py -> build\lib\xlwt\examples
copying xlwt\examples\merged0.py -> build\lib\xlwt\examples
copying xlwt\examples\merged1.py -> build\lib\xlwt\examples
copying xlwt\examples\mini.py -> build\lib\xlwt\examples
copying xlwt\examples\num_formats.py -> build\lib\xlwt\examples
copying xlwt\examples\outline.py -> build\lib\xlwt\examples
copying xlwt\examples\panes.py -> build\lib\xlwt\examples
copying xlwt\examples\parse-fmla.py -> build\lib\xlwt\examples
copying xlwt\examples\protection.py -> build\lib\xlwt\examples
copying xlwt\examples\python.bmp -> build\lib\xlwt\examples
copying xlwt\examples\row_styles.py -> build\lib\xlwt\examples
copying xlwt\examples\row_styles_empty.py -> build\lib\xlwt\examples
copying xlwt\examples\simple.py -> build\lib\xlwt\examples
copying xlwt\examples\sst.py -> build\lib\xlwt\examples
copying xlwt\examples\unicode0.py -> build\lib\xlwt\examples
copying xlwt\examples\unicode1.py -> build\lib\xlwt\examples
copying xlwt\examples\unicode2.py -> build\lib\xlwt\examples
copying xlwt\examples\wsprops.py -> build\lib\xlwt\examples
copying xlwt\examples\xlwt_easyxf_simple_demo.py -> build\lib\xlwt\examples
running install_lib
creating D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt
copying build\lib\xlwt\antlr.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt
copying build\lib\xlwt\BIFFRecords.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt
copying build\lib\xlwt\Bitmap.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt
copying build\lib\xlwt\Cell.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt
copying build\lib\xlwt\Column.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt
copying build\lib\xlwt\CompoundDoc.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt
creating D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\doc
copying build\lib\xlwt\doc\xlwt.html -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\doc
creating D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\examples\big-16Mb.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\examples\big-35Mb.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\examples\blanks.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\examples\col_width.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\examples\country.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\examples\dates.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\examples\format.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\examples\formulas.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\examples\formula_names.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\examples\hyperlinks.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\examples\image.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\examples\merged.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\examples\merged0.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\examples\merged1.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\examples\mini.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\examples\num_formats.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\examples\outline.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\examples\panes.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\examples\parse-fmla.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\examples\protection.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\examples\python.bmp -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\examples\row_styles.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\examples\row_styles_empty.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\examples\simple.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\examples\sst.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\examples\unicode0.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\examples\unicode1.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\examples\unicode2.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\examples\wsprops.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\examples\xlwt_easyxf_simple_demo.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples
copying build\lib\xlwt\ExcelFormula.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt
copying build\lib\xlwt\ExcelFormulaLexer.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt
copying build\lib\xlwt\ExcelFormulaParser.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt
copying build\lib\xlwt\ExcelMagic.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt
copying build\lib\xlwt\Formatting.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt
copying build\lib\xlwt\Row.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt
copying build\lib\xlwt\Style.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt
copying build\lib\xlwt\UnicodeUtils.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt
copying build\lib\xlwt\Utils.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt
copying build\lib\xlwt\Workbook.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt
copying build\lib\xlwt\Worksheet.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt
copying build\lib\xlwt\__init__.py -> D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\antlr.py to antlr.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\BIFFRecords.py to BIFFRecords.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\Bitmap.py to Bitmap.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\Cell.py to Cell.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\Column.py to Column.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\CompoundDoc.py to CompoundDoc.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples\big-16Mb.py to big-16Mb.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples\big-35Mb.py to big-35Mb.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples\blanks.py to blanks.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples\col_width.py to col_width.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples\country.py to country.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples\dates.py to dates.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples\format.py to format.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples\formulas.py to formulas.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples\formula_names.py to formula_names.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples\hyperlinks.py to hyperlinks.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples\image.py to image.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples\merged.py to merged.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples\merged0.py to merged0.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples\merged1.py to merged1.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples\mini.py to mini.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples\num_formats.py to num_formats.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples\outline.py to outline.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples\panes.py to panes.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples\parse-fmla.py to parse-fmla.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples\protection.py to protection.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples\row_styles.py to row_styles.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples\row_styles_empty.py to row_styles_empty.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples\simple.py to simple.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples\sst.py to sst.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples\unicode0.py to unicode0.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples\unicode1.py to unicode1.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples\unicode2.py to unicode2.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples\wsprops.py to wsprops.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\examples\xlwt_easyxf_simple_demo.py to xlwt_easyxf_simple_demo.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\ExcelFormula.py to ExcelFormula.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\ExcelFormulaLexer.py to ExcelFormulaLexer.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\ExcelFormulaParser.py to ExcelFormulaParser.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\ExcelMagic.py to ExcelMagic.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\Formatting.py to Formatting.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\Row.py to Row.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\Style.py to Style.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\UnicodeUtils.py to UnicodeUtils.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\Utils.py to Utils.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\Workbook.py to Workbook.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\Worksheet.py to Worksheet.pyc
byte-compiling D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt\__init__.py to __init__.pyc
running install_egg_info
Writing D:\tmp\WordPress\DevRoot\Python27\Lib\site-packages\xlwt-0.7.4-py2.7.egg-info

然后继续参考:

https://secure.simplistix.co.uk/svn/xlwt/trunk/README.html

去试试示例代码:

import xlwt;
from datetime import datetime;

style0 = xlwt.easyxf('font: name Times New Roman, color-index red, bold on',num_format_str='#,##0.00');
style1 = xlwt.easyxf(num_format_str='D-MMM-YY');

wb = xlwt.Workbook();
ws = wb.add_sheet('A Test Sheet');

ws.write(0, 0, 1234.56, style0);
ws.write(1, 0, datetime.now(), style1);
ws.write(2, 0, 1);
ws.write(2, 1, 1);
ws.write(2, 2, xlwt.Formula("A3+B3"));

wb.save('example.xls');

然后是可以正常执行,生成对应的excel文件的,效果为:

generated excel effect

接下来,就是如何利用此库的事情了。

 

【总结】

想要在Python中,将数据导出到Excel文件中,可以利用xlwt,很是方便。在此感谢作者们。

转载请注明:在路上 » 【记录】Python中生成(写入数据到)Excel文件中

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (1)

    85 queries in 0.182 seconds, using 22.18MB memory