python3.6 xlwt 安装报错解决
2016-08-18 18:10
405 查看
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 <module>
import xlwt
File "C:\Users\l1600293\AppData\Local\Programs\Python\Python36\lib\site-packages\xlwt\__init__.py", line 4, in <module>
from .Worksheet import Worksheet
File "C:\Users\l1600293\AppData\Local\Programs\Python\Python36\lib\site-packages\xlwt\Worksheet.py", line 38, in <module>
from .Row import Row
File "C:\Users\l1600293\AppData\Local\Programs\Python\Python36\lib\site-packages\xlwt\Row.py", line 7, in <module>
from . import ExcelFormula
File "C:\Users\l1600293\AppData\Local\Programs\Python\Python36\lib\site-packages\xlwt\ExcelFormula.py", line 3, in <module>
from . import ExcelFormulaParser, ExcelFormulaLexer
File "C:\Users\l1600293\AppData\Local\Programs\Python\Python36\lib\site-packages\xlwt\ExcelFormulaLexer.py", line 53, in <module>
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' 内容如下
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')
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 <module>
import xlwt
File "C:\Users\l1600293\AppData\Local\Programs\Python\Python36\lib\site-packages\xlwt\__init__.py", line 4, in <module>
from .Worksheet import Worksheet
File "C:\Users\l1600293\AppData\Local\Programs\Python\Python36\lib\site-packages\xlwt\Worksheet.py", line 38, in <module>
from .Row import Row
File "C:\Users\l1600293\AppData\Local\Programs\Python\Python36\lib\site-packages\xlwt\Row.py", line 7, in <module>
from . import ExcelFormula
File "C:\Users\l1600293\AppData\Local\Programs\Python\Python36\lib\site-packages\xlwt\ExcelFormula.py", line 3, in <module>
from . import ExcelFormulaParser, ExcelFormulaLexer
File "C:\Users\l1600293\AppData\Local\Programs\Python\Python36\lib\site-packages\xlwt\ExcelFormulaLexer.py", line 53, in <module>
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')
相关文章推荐
- Python-json
- python 字符编码
- 使用Python实现Map Reduce程序
- python __call__ 内置函数的使用
- Python入门_01安装与简介
- python小记(2)
- python subprocess.Popen 非阻塞
- ndnSIM 2.0 因缺少pythonbind无法使用visual组件问题 —pybindgen (found '') ".. ns3::VisualSimulatorImpl not found"
- R语言与Python对比
- 使用python+机器学习方法进行情感分析(详细步骤)
- python爬取贴吧关注人数数据
- Anaconda python各模块简介
- Python入门:全站url爬取
- python的PIL工具包入门
- Python 源码包编译 安装(一般源码安装通用)
- 初学Python(二)——数组
- 基于不同的数据源提供统一的RESTful风格的API
- 初学Python(一)——数据类型
- Python爬虫入门——URLError异常处理
- 简单工厂模式(python版)