在使用Python写入数据到Excel表格中时出现报错信息记录:“NotImplementedError: formatting_info=True not yet implemented”

报错分析:看报错信息是未实现的错,其实就是版本不兼容

我在代码中写的是使用xlrd库的方法进行Excel处理,但是我创建的Excel是office 2016版本的,而xlrd只支持2007以前的版本,导致不兼容报错

解决办法1:将模板文件另存为Excel 2003版本的文件格式

解决方法2:使用Python的openpyxl库中的方法进行编写代码

xlrd库与openpyxl库中使用的一些方法说明

(1)、打开一个Excel文件

xlrd中用open_workbook方法:

def open_workbook(filename=None,
logfile=sys.stdout,
verbosity=,
use_mmap=USE_MMAP,
file_contents=None,
encoding_override=None,
formatting_info=False,
on_demand=False,
ragged_rows=False):

open_workbook方法中参数说明:

  • filename参数:是要打开文件的路径及名称
  • logfile:写入消息和诊断的打开文件
  • verbosity:增加写入的日志文件
  • use_mmap:是否使用mmap模块是用启发式方法,如果存在,则使用mmap
  • file_contents:使用了该参数就不要使用filename
  • formatting_info:默认值是“False”,当“True”时,格式信息将从电子表格中读取文件。
  • ragged_rows:默认值“False”表示所有行都用空单元格填充,“True” 表示行的末尾没有空单元格。

openpyxl中用load_workbook方法:

def load_workbook(filename, read_only=False, keep_vba=KEEP_VBA,
data_only=False, keep_links=True)

load_workbook方法中的参数说明:

  • filename:打开文件的路径或对象
  • read_only:为读取而优化,内容无法编辑,false时可以编辑,true时无法编辑
  • keep_vba:保留vba内容
  • data_only:控制带有公式的单元格是具有公式(默认值)还是具有上次Excel读取工作表时存储的值
  • keep_links:是否应保留指向外部工作簿的链接。默认值为True

(2)、复制一个文件都用copy,保存文件都用save

(3)、添加一个sheet工作表

xlrd中用add_sheet方法:

def add_sheet(self, sheetname, cell_overwrite_ok=False):

add_sheet方法参数说明:

  • sheetname:工作表的名称
  • cell_overwrite_ok:默认是false,如果是“True”,则添加的工作表中的单元格将不会因为多次写入时出现异常。

openpyxl中用create_sheet方法:

def create_sheet(self, title=None, index=None):

create_sheet方法中的参数说明,就两个参数:

  • title:插入工作表的名称,str类型
  • index:从第几个开始插入,索引从0开始

(4)、写入数据到Excel表格中

xlrd中用write方法:

 def write(self, r, c, label="", style=Style.default_style):

write方法参数说明:

  • r参数:写入的行
  • c参数:写入的列
  • label:要写入的数据值
  • style:写入的样式

openpyxl中用cell方法:

 def cell(self, row, column, value=None):

cell方法参数说明:

  • row:写入单元格的行
  • column:写入单元格的列
  • value:写入单元格的值

最后贴上两种不同库写入Excel数据的代码:

(1)使用Python的xlrd库实现导出数据到Excel中

    def export_excel(self, names_export):
"""
:param names_export:
:param name_export: 待导出的接口名称,列表形式
:return:
"""
counts_export = len(names_export) # 导出总数
fail_export = [] # 导出失败接口名列表
try:
src = open_workbook(config.src_path+'/report/report_module.xls', formatting_info=True)
destination = copy(src)
dt = datetime.datetime.now().strftime("%Y%m%d%H%M%S") # 当前时间戳
file_path = config.src_path+'/report/'+str(dt)+'.xls'
destination.save(file_path) # 保存模板表格到新的目录下
for name_interface in names_export:
case_interface = operation_db.select_all("select * from case_interface where case_status=1 and "
"name_interface='%s'" % name_interface)
if len(case_interface['data']) != 0 and case_interface['code'] == '':
src = open_workbook(file_path, formatting_info=True)
destination = copy(src)
sheet = destination.add_sheet(name_interface, cell_overwrite_ok=True)
for col in range(0, len(self.field)):
sheet.write(0, col, self.field[col]) # 获取并写数据段信息到sheet中
for row in range(1, len(case_interface['data'])+1):
for col in range(0, len(self.field)):
sheet.write(row, col, '%s' % case_interface['data'][row-1][col]) # 写数据到对应的Excel表中
destination.save(file_path)
elif len(case_interface['data']) == 0 and case_interface['code'] == '':
fail_export.append(name_interface)
else:
fail_export.append(name_interface)
result = {'code': '', 'message': '导出总数%s,失败数:%s' % (counts_export, len(fail_export)),
'data': fail_export}
except Exception as e:
result = {'code': '', 'message': '导出过程异常|导出总数:%s,失败数:%s'
% (counts_export, len(fail_export)), 'data': fail_export}
logging.basicConfig(filename=config.src_path+'/log/syserror.log', level=logging.DEBUG,
format='%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s %(message)s')
logger = logging.getLogger(__name__)
logger.exception(e)
return result

(2)使用Python的openpyxl库中的方法实现写入数据到Excel中

    def export_excel(self, names_export):
"""
:param names_export:
:param name_export: 待导出的接口名称,列表形式
:return:
"""
counts_export = len(names_export) # 导出总数
fail_export = [] # 导出失败接口名列表
try:
# src = open_workbook(config.src_path+'/report/report_module.xls', formatting_info=True)
src = load_workbook(config.src_path + '/report/report_module.xlsx', read_only=False)
destination = copy(src)
dt = datetime.datetime.now().strftime("%Y%m%d%H%M%S") # 当前时间戳
file_path = config.src_path+'/report/'+str(dt)+'.xlsx'
destination.save(file_path) # 保存模板表格到新的目录下
for name_interface in names_export:
case_interface = operation_db.select_all("select * from case_interface where case_status=1 and "
"name_interface='%s'" % name_interface)
if len(case_interface['data']) != 0 and case_interface['code'] == '':
src = load_workbook(file_path, read_only=False)
destination = copy(src)
sheet = destination.create_sheet(name_interface, 0)
for col in range(1, len(self.field)+1):
sheet.cell(1, col, self.field[col-1]) # 获取并写数据段信息到sheet中
for row in range(2, len(case_interface['data'])+2):
for col in range(1, len(self.field)+1):
sheet.cell(row, col, '%s' % case_interface['data'][row-2][col-1]) # 写数据到对应的Excel表中
destination.save(file_path)
elif len(case_interface['data']) == 0 and case_interface['code'] == '':
fail_export.append(name_interface)
else:
fail_export.append(name_interface)
result = {'code': '', 'message': '导出总数%s,失败数:%s' % (counts_export, len(fail_export)),
'data': fail_export}
except Exception as e:
result = {'code': '', 'message': '导出过程异常|导出总数:%s,失败数:%s'
% (counts_export, len(fail_export)), 'data': fail_export}
logging.basicConfig(filename=config.src_path+'/log/syserror.log', level=logging.DEBUG,
format='%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s %(message)s')
logger = logging.getLogger(__name__)
logger.exception(e)
return result

Python导出数据到Excel表格-NotImplementedError: formatting_info=True not yet implemented的更多相关文章

  1. Python模块学习之xlrd 读取Excel时传入formatting_info=True报错:NotImplementedError: formatting_info=True not yet implemented

    问题:xlrd读取Excel时传入 formatting_info=True 报错 之前我们使用读取xls文件的时候都是使用的xlrd库,但是这个库只能操作 .xls格式,对于后来的 .xlsx的版本 ...

  2. 导出数据到Excel表格

    开发工具与关键技术:Visual Studio 和 ASP.NET.MVC,作者:陈鸿鹏撰写时间:2019年5月25日123下面是我们来学习的导出数据到Excel表格的总结首先在视图层写导出数据的点击 ...

  3. Java操作Jxl实现导出数据生成Excel表格数据文件

    实现:前台用的框架是Easyui+Bootstrap结合使用,需要引入相应的Js.Css文件.页面:Jsp.拦截请求:Servlet.逻辑处理:ClassBean.数据库:SQLserver. 注意: ...

  4. python 导出数据到excel 中,一个好用的导出数据到excel模块,XlsxWriter

    最近公司有项目需要导出数据到excel,首先想到了,tablib,xlwt,xlrd,xlwings,win32com[还可以操作word],openpyxl,等模块但是 实际操作中tablib 写入 ...

  5. PHP批量导出数据为excel表格

    之前用插件phoexcel写过批量导入数据,现在用到了批量导出,就记录一下,这次批量导出没用插件,是写出一个表格,直接输出 //$teacherList 是从数据库查出来的二维数组 $execlnam ...

  6. php动态导出数据成Excel表格

    一.封装 Excel 导出类 include/components/ExecExcel.php <?php /*** * @Excel 导入导出类. */ class ExecExcel { / ...

  7. spring boot 使用POI导出数据到Excel表格

    在spring boot 的项目经常碰到将数据导出到Excel表格的需求,而POI技术则对于java操作Excel表格提供了API,POI中对于多种类型的文档都提供了操作的接口,但是其对于Excel表 ...

  8. Excel VBA ——如何导出数据到excel表格

    sub OutPut() Dim FileTitle, MyPath, MyFullName As String Application.ScreenUpdating = false '关闭表格公式的 ...

  9. Java导出数据生成Excel表格

    事先准备: 工具类: package com.wazn.learn.util.export; import java.sql.Connection; import java.sql.DriverMan ...

随机推荐

  1. TensorFlow 趣题

    checkpoint 文件夹 Tensorflow训练后的模型可以保存checkpoint文件,checkpoint文件是结构与权重分离的四个文件,便于训练. 1)checkpoint 文件 保存断点 ...

  2. 8千字干货教程|java反射精讲

    java反射机制精讲 目录 1. 反射机制的概念 2. 反射的基础Class类 3. 反射的用法 4. 反射的应用示例 作者简介:全栈学习笔记,一个正在努力的人 微信公众号:公众号日更,精彩美文每天推 ...

  3. 终极解决方案之——Centos7由于误删或更新python导致 No module named yum

    之前由于不懂yum和python之间的关系,因为一直在学python3,看到系统里/usr/lib下的python2我就直接删了,结果... 可能还有人是因为python升级的原因,即系统自带的pyt ...

  4. [源码分析] 从FlatMap用法到Flink的内部实现

    [源码分析] 从FlatMap用法到Flink的内部实现 0x00 摘要 本文将从FlatMap概念和如何使用开始入手,深入到Flink是如何实现FlatMap.希望能让大家对这个概念有更深入的理解. ...

  5. 答应我,不会这些概念,简历不要写 “熟悉” zookeeper

    整理了一些Java方面的架构.面试资料(微服务.集群.分布式.中间件等),有需要的小伙伴可以关注公众号[程序员内点事],无套路自行领取 一口气说出 9种 分布式ID生成方式,面试官有点懵了 面试总被问 ...

  6. npm git 常用命令行 记录

    1. 推出node命令行: 两次ctrl+C或者一次ctrl+D    退出终端:exit; 2.npm 常用 npm install <name>  安装包 npm install &l ...

  7. 2020 | 可替代Selenium的测试框架Top15

    本文首发于 微信公众号: 软测小生 Selenium是一种开源自动测试工具.它可以跨不同的浏览器和平台在Web应用程序上执行功能,回归,负载测试.Slenium是最好的工具之一,但确实有一些缺点. 业 ...

  8. 2020.4.4号全国疫情哀悼日网页变灰色前端是如何实现的?-pink老师

    今天是4.4疫情哀悼日,纪念疫情期间牺牲的烈士和逝世同胞,因此大部分网站颜色都变灰色了,我们前端是如何实现的呢? 核心原理,使用css3的滤镜效果即可,filter grayscale 将整个界面变为 ...

  9. Kubernets中获取客户端真实IP总结

    1. 导言 绝大多数业务场景都是需要知道客户端IP的 在k8s中运行的业务项目,如何获取到客户端真实IP? 本文总结了通行的2种方式 要答案的直接看方式一.方式二和总结 SEO 关键字 nginx i ...

  10. Java并发基础05. 传统线程同步通信技术

    先看一个问题: 有两个线程,子线程先执行10次,然后主线程执行5次,然后再切换到子线程执行10,再主线程执行5次--如此往返执行50次. 看完这个问题,很明显要用到线程间的通信了, 先分析一下思路:首 ...