想要试试,在Python中,如何将数据,导出为Excel,即把数据写入到新生成的excel文件。
1.网上看到:
Working with Excel Files in Python
其中包括,Python中,如何读取excel文件,如何写入数据到excel文件等等相关的库。
看起来应该是这方面的资料中,总结的最好的了.
2.此处暂时只需要写入数据到Excel,所以就去参考:
去看看示例代码:
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去安装:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | 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
去试试示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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文件的,效果为:
接下来,就是如何利用此库的事情了。
【总结】
想要在Python中,将数据导出到Excel文件中,可以利用xlwt,很是方便。在此感谢作者们。
转载请注明:在路上 » 【记录】Python中生成(写入数据到)Excel文件中