python3.6 版本
pip install xlwt 安装成功后
import xlwt 一直提示错误
Python 3.6.0a4 (v3.6.0a4:017cf260936b, Aug 16 2016, 00:59:16) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
======= RESTART: E:\work\officedoc\bom\code\python\test_excel3xlwt.py =======
Traceback (most recent call last):
File "E:\work\officedoc\bom\code\python\test_excel3xlwt.py", line 6, in
import xlwt
File "C:\Users\l1600293\AppData\Local\Programs\Python\Python36\lib\site-packages\xlwt\__init__.py", line 4, in
from .Worksheet import Worksheet
File "C:\Users\l1600293\AppData\Local\Programs\Python\Python36\lib\site-packages\xlwt\Worksheet.py", line 38, in
from .Row import Row
File "C:\Users\l1600293\AppData\Local\Programs\Python\Python36\lib\site-packages\xlwt\Row.py", line 7, in
from . import ExcelFormula
File "C:\Users\l1600293\AppData\Local\Programs\Python\Python36\lib\site-packages\xlwt\ExcelFormula.py", line 3, in
from . import ExcelFormulaParser, ExcelFormulaLexer
File "C:\Users\l1600293\AppData\Local\Programs\Python\Python36\lib\site-packages\xlwt\ExcelFormulaLexer.py", line 53, in
VERBOSE+LOCALE+IGNORECASE)
File "C:\Users\l1600293\AppData\Local\Programs\Python\Python36\lib\re.py", line 223, in compile
return _compile(pattern, flags)
File "C:\Users\l1600293\AppData\Local\Programs\Python\Python36\lib\re.py", line 292, in _compile
p = sre_compile.compile(pattern, flags)
File "C:\Users\l1600293\AppData\Local\Programs\Python\Python36\lib\sre_compile.py", line 555, in compile
p = sre_parse.parse(p, flags)
File "C:\Users\l1600293\AppData\Local\Programs\Python\Python36\lib\sre_parse.py", line 793, in parse
p.pattern.flags = fix_flags(str, p.pattern.flags)
File "C:\Users\l1600293\AppData\Local\Programs\Python\Python36\lib\sre_parse.py", line 770, in fix_flags
raise ValueError("cannot use LOCALE flag with a str pattern")
ValueError: cannot use LOCALE flag with a str pattern
查找网上,copy xlwt解压包内容到lib下面,也不行。
下班后,依然没有措施。进入sre_parse.py进入查看错误提示770行,
修改代码如下
if flags & SRE_FLAG_LOCALE:
pass #stone20160818 raise ValueError("cannot use LOCALE flag with a str pattern")
执行import xlwt,结果ok。
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')
Windows文件夹生成'example.xls' 内容如下
1,234.56 | |||
18-Aug-16 | |||
1 | 1 | 2 |
3.6版本 在同一code中读写同一excel,会提示权限错误(open文件)。(更换不同文件名后,改回来问题不重现了)
必须读和写是不同的文档名。这2个文档可以已经存在。
另外在保存文件后 .xls格式可以打开,保存的 .xlsx格式打开出错。
对.xlsx的文件读取正确
#-*- coding: utf8 -*-
import xlrd
import xlutils
import xlwt
from xlrd import open_workbook
from xlutils.copy import copy
#from datetime import datetime
#fname = "E:/work/officedoc/bom/code/python/reflect.xlsx"
#bk = xlrd.open_workbook(fname)
bk = open_workbook('E:/work/officedoc/bom/code/python/reflect.xls')
shxrange = range(bk.nsheets)
try:
sh = bk.sheet_by_name("sheet1")
except:
print( "no sheet in %s named sheet1" % fname)
#获取行数
nrows = sh.nrows
#获取列数
ncols = sh.ncols
print( "nrows %d, ncols %d" % (nrows,ncols))
#获取第一行第一列数据
cell_value = sh.cell_value(1,1)
#print cell_value
print("cell_value[1,1]=%s" % cell_value)
row_list = []
#获取各行数据
for i in range(1,nrows):
row_data = sh.row_values(i)
row_list.append(row_data)
print ("row_list: ",row_list)
'''
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 = copy(bk)
ws = wb.get_sheet(0) #通过get_sheet()获取的sheet有write()方法
'''
ws.write(10, 0, 1234.56, style0)
ws.write(11, 0, datetime.now(), style1)
'''
ws.write(10, 0, 1234.56)
#ws.write(11, 0, datetime.now())
ws.write(12, 0, 1)
ws.write(12, 1, 1)
ws.write(12, 2, 2)
wb.save('E:/work/officedoc/bom/code/python/reflect2.xls')