Python 判断是否存在Excel表,无则生成,有则删除重建

import os
import xlwt
from openpyxl import workbook def sheet_method(work_book, add_sheet=[]):
wk = work_book
# rename default sheet
ss_sheet = wk.get_sheet_by_name('Sheet')
ss_sheet.title = add_sheet[] for i in range(, len(add_sheet)):
# add new sheet
wk.create_sheet(add_sheet[i]) # switch to active sheet
sheet_num = wk.get_sheet_names()
last_sheet = len(sheet_num) -
sheet_index = sheet_num.index(sheet_num[last_sheet])
wk.active = sheet_index def create_excel(filename="filename.xlsx", recreate=False, add_sheet=[]):
"""
Judge whether there is exist appointed Excel file, create it if not exist, otherwise delete and recreate it
:param filename:
:return:
""" # .xls后缀结尾的只能用xlwt模块写,只能写2的16次方即65,535条数据
# path = os.path.join(os.getcwd()+r'\test.xls')
# .xlsx的只能用openpyxl模块写,这模块能写2的32次方即4,,,296的数据
path = os.path.join(os.getcwd() + "\\" + filename + ".xlsx")
p1 = os.path.exists(path)
if p1:
if recreate:
os.remove(path)
wk = workbook.Workbook()
sheet_method(wk, add_sheet)
# save workbook
wk.save(path)
print("Had been deleted and recreated " + filename)
else:
# error_info = 'Exist ' + filename + ' in path: ' + path\
# + " Please use another filename, otherwise the "\
# + filename + " file will be override"\
# + " if you need override please set recreate as Ture"
# raise Exception, error_info
pass
else:
# filename = xlwt.Workbook()
wk = workbook.Workbook()
sheet_method(wk, add_sheet)
# save workbook
wk.save(path)
# print("you had create " + filename) create_excel("test2222", recreate=True, add_sheet=["rebuild3", "rebuild4"])

Python 判断是否存在Excel表的更多相关文章

  1. Python xlrd模块读取Excel表中的数据

    1.xlrd库的安装 直接使用pip工具进行安装(当然也可以使用pycharmIDE进行安装,这里就不详述了) pip install xlrd 2.xlrd模块的一些常用命令 ①打开excel文件并 ...

  2. Python+Selenium进行UI自动化测试项目中,常用的小技巧1:读取excel表,转化成字典(dict)输出

    从今天开始我将会把在项目中遇到的问题,以及常用的一些技巧来分享出来,以此来促进自己的学习和提升自己:更加方便我以后的查阅. 现在要说的是:用Python来读取excel表的数据,返回字典(dict), ...

  3. python中Excel表操作

    python中关于excel表个的操作 使用 python中的xlwt和xlrd模块进行操作 # 2003之前:Excel:xls# 2003之后:Excel:xlsx# xlrd:读取的模块:xls ...

  4. python实现导出excel表(前端+后端)

    之前在做项目管理系统的时候需要实现将数据导出到excel表的功能,搜索之后发现了python的xlwt模块可以很好的实现这项功能. 首先是导入xlwt模块: import xlwtfrom io im ...

  5. python 之 处理excel表的xlwt模块学习记录

    python 操作excel表的常用模块主要有2个: 1:xlrd:读取excel表 2:xlwt:创建并写入excel表 安装方法: 可以直接下载安装:https://pypi.python.org ...

  6. excel表如何实现多if选择结构多分支判断

    excel表如何实现多if选择结构多分支判断 一.总结 一句话总结:把多if分支转换成单if分支相加. 也可以if分支,也可以lookup函数. 1.CHOICE: +2 if band A; +1 ...

  7. python openpyxl模块实现excel的读取,新表创建及原数据表追加新数据

    当实际工作需要把excel表的数据读取出来,或者把一些统计数据写入excel表中时,一个设计丰富,文档便于寻找的模块就会显得特别的有吸引力,本文对openpyxl模块的一些常见用法做一些记录,方便工作 ...

  8. 用python库openpyxl操作excel,从源excel表中提取信息复制到目标excel表中

    现代生活中,我们很难不与excel表打交道,excel表有着易学易用的优点,只是当表中数据量很大,我们又需要从其他表册中复制粘贴一些数据(比如身份证号)的时候,我们会越来越倦怠,毕竟我们不是机器,没法 ...

  9. python xlrd 模块(获取Excel表中数据)

    python xlrd 模块(获取Excel表中数据) 一.安装xlrd模块   到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了pyt ...

随机推荐

  1. bzoj1047 [HAOI2007]理想的正方形——二维单调队列

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1047 就是先对行做一遍单调队列,再对那个结果按列做一遍单调队列即可. 代码如下: #incl ...

  2. P3398 仓鼠找sugar 又一次血的教训

    做什么题都要注意数组的大小,不要犯下数组越界的错误(温馨(狠心)提示): 做了好多遍就是不对,原来是[20]的数组,在for下循环1——>20,神奇爆零: 链接:https://www.luog ...

  3. Linux 系统管理命令 - iftop - 动态显示网络接口流量信息

    命令详解 重要星级: ★★★★☆ 功能说明: iftop 是一款实时流量监控工具,可用于监控 TCP/IP 连接等,必须以 root 用户的身份运行.一般最小化安装系统是没有这个命令的,需要使用 yu ...

  4. python 面向对象六 类属性和实例属性

    一.实例属性 Python是动态语言,根据类创建的实例可以任意绑定属性. >>> class Student(object): ... def __init__(self, name ...

  5. 洛谷P3569 [POI2014]KAR-Cards(线段树)

    传送门 蠢了…… 我们用线段树,记$w0$为该区间最左端取小值时,最右端最小能取大还是小还是无解,$w1$表示最左端取大值时,最右端最小能取大还是小还是无解 然后只要把交换看做修改就好了 这么说可能很 ...

  6. python爬虫爬取汽车页面信息,并附带分析(静态爬虫)

    环境: windows,python3.4 参考链接: https://blog.csdn.net/weixin_36604953/article/details/78156605 代码:(亲测可以运 ...

  7. SetViewportOrgEx(视口) 与 SetWindowOrgEx(窗口) 解析

    SetViewportOrgEx (hdc,x/2,y/2) 视口中心点设置到窗口中心 获取客户群x和y,x/2,y/2 ,这样中心点就到窗口中间了. SetWindowOrgEx(hdc,-x/2, ...

  8. Android 线程池系列教程(5)与UI线程通信要用Handler

    Communicating with the UI Thread 上一课 下一课 1.This lesson teaches you to Define a Handler on the UI Thr ...

  9. 转 SQLPLUS中SQL换行执行

    权声明:本文为博主原创文章,未经博主允许不得转载. 正常情况下,在SQLPLUS中输入命令时,可以换行,但不能有空格,否则不能执行,会直接返回到SQL>下.但通过命令设置可以实现语句换行时允许有 ...

  10. git免密码

    法1: git config --global credential.helper store 这样就自动储存密码 法2: 使用ssh访问(https:// 改成 ssh://)