python 操作Excel表格,解压zip包,压缩zip包,目录遍历
import zipfile
import os,shutil
import openpyxl file_list_pos=""
fileName=""
zipfileName=""
def un_zip(file_name):
"""unzip zip file"""
zip_file = zipfile.ZipFile(file_name)
if os.path.isdir(file_name + "_files"):
pass
else:
os.mkdir(file_name + "_files")
for names in zip_file.namelist():
zip_file.extract(names,file_name + "_files/")
zip_file.close()
def cd_to_filelist(cur_dir):
file_list=os.listdir(cur_dir)
for i in file_list:
if(os.path.isdir(i)):
os.chdir(i)
cd_to_filelist(os.getcwd())
else:
if(os.path.splitext(i)[]==".xlsx"):
global fileName
fileName=i
elif(os.path.splitext(i)[]==".zip"):
global zipfileName
zipfileName=i
else:
pass
global file_list_pos
file_list_pos=os.getcwd()
def rewrite():
f=open("KONTRAK.txt","r")
secondline=f.readline()
secondline=f.readline()
account=secondline.split("|")[:]
os.chdir(current_dir)
f.close()
file_name="LAPORAN HASIL PEMERIKSAAN - J23_1 28886.txt"
f=open('tmp\\'+ file_name,"r")
lines = f.readlines()
lastline=lines[-]
newline="|".join(account)+"|"+"|".join(lastline.split("|")[:])
file = open(file_name, 'w+')
for i in lines[:-]:
file.write(i)
file.write(newline)
file.close()
#f = zipfile.ZipFile(os.path.splitext(file_name)[]+'.zip','w',zipfile.ZIP_STORED)
#f.write(file_name)
#f.close()
#os.remove(file_name)
def write07Excel(path):
wb = openpyxl.Workbook()
sheet = wb.active
sheet.title = 'Sheet1'
for i in rowlists:
if(i[-]=="Gender"):
for k in temp:
if(k[]=="RESULT"):
for l in k:
i.append(l)
else:
pass
elif((i[-]=="P")or(i[-]=="L")):
for k in temp:
if(k[]=="PASS"):
for l in k:
i.append(l)
else:
pass
else:
for j in range():
i.append(None)
for i in range(,len(rowlists)):
for j in range(, len(rowlists[i])):
sheet.cell(row=i+, column=j+, value=rowlists[i][j])
wb.save(path)
print("Done!")
def read07Excel(path):
wb = openpyxl.load_workbook(path)
sheet = wb['Sheet1']
rowlist=[]
for row in sheet.rows:
cc=[]
for cell in row:
cc.append(cell.value)
#print(cell.value, "\t", end="")
rowlist.append(cc)
#print("\n")
return rowlist
def delfold(cur_dir):
file_list=os.listdir(cur_dir)
for i in file_list:
if(os.path.splitext(i)[]!=".zip"):
shutil.rmtree(cur_dir+"\\"+i)
current_dir=os.getcwd()
os.chdir("file")
cd_to_filelist(os.getcwd())
un_zip(zipfileName)
cd_to_filelist(os.getcwd())
rewrite()
#src
absp=file_list_pos+"\\"+fileName
#temp
tempath=".\\tmp\\common.xlsx"
#des
despath=fileName
rowlists=read07Excel(absp)
temp=read07Excel(tempath)
write07Excel(despath)
delfold(os.getcwd()+"\\file")
python 操作Excel表格,解压zip包,压缩zip包,目录遍历的更多相关文章
- 【转】python操作excel表格(xlrd/xlwt)
[转]python操作excel表格(xlrd/xlwt) 最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异, ...
- python - 操作excel表格
说明:由于公司oa暂缺,人事妹子在做考勤的时候,需要通过几个excel表格去交叉比对员工是否有旷工或迟到,工作量大而且容易出错. 这时候it屌丝的机会来啦,花了一天时间给妹子撸了一个自动化脚本. 1. ...
- 转载:python操作excel表格(xlrd/xlwt)
python操作excel表格(xlrd/xlwt) 最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异,而 ...
- Python 利用Python操作excel表格之openyxl介绍Part2
利用Python操作excel表格之openyxl介绍 by:授客 QQ:1033553122 欢迎加入全国软件测试交流qq群(群号:7156436) ## 绘图 c = LineChart() ...
- Python 利用Python操作excel表格之openyxl介绍Part1
利用Python操作excel表格之openyxl介绍 by:授客 QQ:1033553122 欢迎加入全国软件测试交流qq群(群号:7156436),免费获取以下性能监控工具(类似Nmon精简版) ...
- Python 利用Python操作excel表格之xlwt介绍
利用Python操作excel表格之xlwt介绍 by:授客 QQ:1033553122 直接上代码 案例1 #!/usr/bin/env python # -*- coding:utf-8 ...
- python操作excel表格(xlrd/xlwt)
最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异,而且不太能满足需求,不过经过一番对源码的"研究&q ...
- Python操作excel表格
用Python操作Excel在工作中还是挺常用的,因为毕竟不懂Excel是一个用户庞大的数据管理软件 注:本篇代码在Python3环境下运行 首先导入两个模块xlrd和xlwt,xlrd用来读取Exc ...
- 28.python操作excel表格(xlrd/xlwt)
python读excel——xlrd 这个过程有几个比较麻烦的问题,比如读取日期.读合并单元格内容.下面先看看基本的操作: 首先读一个excel文件,有两个sheet,测试用第二个sheet,shee ...
- Python操作Excel表格,xlwt模块的使用
Python可以操作Excel的模块不止一种,我习惯使用的写入模块是xlwt(一般都是读写模块分开的) 按照模块使用pip install xlwt 就行了,很常规的方式 直接进代码解析,本文源码 w ...
随机推荐
- Unity3D笔记六 GUI游戏界面
1.Label:标签控件,可以在游戏中用来展示文本字符串信息,不仅可以写字还可以贴图片. 2.Button:按钮控件,一般分图片按钮和普通的按钮,还有一个连续按钮RepeatButton注意,这个在W ...
- Swift - 多线程GCD详解
// GCD详解 // 目录: // 1. 创建GCD队列(最常用) // 2. 自定义创建队列 // 3. 使用多线程实现延迟加载 // 4. 使用多线程实现重复(循环) // 5. ...
- angular -- ng-ui-route路由及其传递参数?script标签版
考虑到 多视图等因素,所以 angular 的路由考虑使用 ng-ui-route来做,而不使用 ng-route来做! <!DOCTYPE html> <html lang=&qu ...
- Asp.net读取和写入txt文件方法(实例)!
Asp.NET读取和写入txt文件方法(实例)! [程序第一行的引入命名空间文件 - 参考] System; using System.Collections; using System.Config ...
- poj3449 Geometric Shapes【计算几何】
含[判断线段相交].[判断两点在线段两侧].[判断三点共线].[判断点在线段上]模板 Geometric Shapes Time Limit: 2000MS Memory Limit: 655 ...
- UA-* headers
HTTP The Definitive Guide Request headers are headers that make sense only in a request message. The ...
- JS实现数字千位符格式化方法
/** * [number_format 参数说明:] * @param {[type]} number [number:要格式化的数字] * @param {[type]} decimals [de ...
- mysql 数据操作 多表查询 子查询 介绍
子查询就是: 把一条sql语句放在一个括号里,当做另外一条sql语句查询条件使用 拿到这个结果以后 当做下一个sql语句查询条件mysql 数据操作 子查询 #1:子查询是将一个查询语句嵌套在另一个 ...
- PhotoSwipe中文API(三)
http://photoswipe.com/documentation/api.html 所有的方法和这个网页上列出的属性是公开的.如果你想看看例子什么API可以做的,拿在默认PhotoSwipe U ...
- JQuery表格操作的常用技巧总结
JQuery对表格进行操作的常用技巧. 1.表格奇数行和偶数行分别添加样式 复制代码代码如下: $(function(){ $('tr:odd').addClass("odd") ...