把有合并单元格的信息读取出来,输出所在层数与位置

我要操作的Excel是这样的

要的到的是这样的效果

# -*- coding: utf-8 -*-
import xlrd
import xlwt
r=1
# shxrange = range(bk.nsheets)
wb = xlwt.Workbook(encoding='utf-8')#创建工作簿,设置字符编码
ws = wb.add_sheet('A TEST Sheet')#创建sheet goods = xlrd.open_workbook('test3.xlsx')#打开文件
index = 0
for sheet_index in range(goods.nsheets):
sh = goods.sheet_by_index(sheet_index)#返回第几页的对象
#添加内容到row_list当中
row_list = []
for rx in range(sh.nrows):
if sh.row(rx)[0].ctype:
print sh.row(rx)
row_list.append(sh.row_values(rx))
# 取出有多少行
row = len(row_list)
# print row
for r in range(row):
weizhi =1
for c in range(len(row_list[r])):
if row_list[r][c]:
ws.write(index, 0, row_list[r][c])
ws.write(index,3,row)
ws.write(index,4,weizhi)
index = index + 1
row =row-1
wb.save('example3.xls')

需要这样操作的数据有很多,就要分为多个sheet页,每个sheet页包含一个要操作的数据,这样的话就可以把每个sheet页重新数层数,如果不需要也可以写在一个sheet页当中,要批量处理就分多个sheet页

要处理的数据和上面差不多,不过稍微麻烦一点

要处理的数据分为两个sheet页

sheet页1



sheet页2

要处理的数据是通过sheet1页每个单元格数据*前面的第一个数字找到sheet2页对应的行号,这样的数据太难看了,想要得到这样的数据

# -*- coding: utf-8 -*-
import xlrd
import xlwt
r=1
# shxrange = range(bk.nsheets)
wb = xlwt.Workbook(encoding='utf-8')#创建工作簿,设置字符编码
ws = wb.add_sheet('A TEST Sheet')#创建sheet goods = xlrd.open_workbook('test10.xlsx')#打开文件
index = 0
sheet = 0
for sheet_index in range(goods.nsheets/2):
sh = goods.sheet_by_index(sheet)#返回第1页的对象
print sheet
#添加内容到row_list当中
row_list = []
for rx in range(sh.nrows):
#第一行为当前页的分类
if sh.row(rx)[0].ctype:
# print sh.row(rx)
row_list.append(sh.row_values(rx))
else:
# print r
r +=1
# 取出有多少行
row = len(row_list)
sheet = sheet+1
print sheet
#将第二页的内容添加到con_list当中
con_list = []
sh = goods.sheet_by_index(sheet)#返回第几页的对象
for rx in range(sh.nrows):
#第一行为当前页的分类
# if rx ==0:
# fenglei = sh.row_values(rx)
# else:
if sh.row(rx)[0].ctype:
# print sh.row(rx)
con_list.append(sh.row_values(rx))
else:
print r
r +=1
sheet=sheet+1
# print row
for r in range(row):
weizhi = 1
for c in range(len(row_list[r])):
if row_list[r][c]:
ws.write(index, 0, row_list[r][c])
# ws.write(index,10,fenglei)
ws.write(index,3,row)
ws.write(index,4,weizhi)
try:
ws.write(index,2,con_list[int(row_list[r][c].split("*")[0])-1][0])
try:
ws.write(index,5,con_list[int(row_list[r][c].split("*")[0])-1][1])
except:
ws.write(index,5,500)
try:
ws.write(index,6,con_list[int(row_list[r][c].split("*")[0])-1][2])
except:
ws.write(index,6,500) ws.write(index,7,row_list[r][c].split("*")[1])
ws.write(index,8,row_list[r][c].split("*")[2])
ws.write(index,9,'hgyingliao'+str(sheet))
except:
ws.write(index,5,500)
ws.write(index,6,500)
ws.write(index,7,500)
ws.write(index,8,500)
ws.write(index,9,'hgyingliao'+str(sheet))
weizhi =weizhi+1
index = index + 1
row =row-1
wb.save('example3.xls')

每两个sheet页为单位进行处理

xlrd,xlwt操作Excel实例的更多相关文章

  1. python第三方库——xlrd和xlwt操作Excel文件学习

    python第三方库——xlrd和xlwt操作Excel文件学习 1安装: C:\Users\Lenovo>pip install xlwtCollecting xlwt  Downloadin ...

  2. C#开发中使用Npoi操作excel实例代码

    C#开发中使用Npoi操作excel实例代码 出处:西西整理 作者:西西 日期:2012/11/16 9:35:50 [大 中 小] 评论: 0 | 我要发表看法 Npoi 是什么? 1.整个Exce ...

  3. xlrd》操作excel 出现的问题:File "D:\python37\lib\site-packages\xlrd\formula.py", line 1150, in evaluate_name_formula assert len(tgtobj.stack) == 1

    xlrd>操作excel  出现的问题 报错如下: D:\python37\python.exe D:/testWang/waimai/tools/get_excelData.py*** for ...

  4. python中使用xlrd、xlwt操作excel表格详解

    python读excel——xlrd 这个过程有几个比较麻烦的问题,比如读取日期.读合并单元格内容.下面先看看基本的操作: 首先读一个excel文件,有两个sheet,测试用第二个sheet,shee ...

  5. 【转】经典!python中使用xlrd、xlwt操作excel表格详解

    最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异,而且不太能满足需求,不过经过一番对源码的"研究&q ...

  6. xlrd、xlwt 操作excel表格详解

    转自:https://www.cnblogs.com/jiablogs/p/9141414.html python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是 ...

  7. python中使用xlrd、xlwt操作excel

    python 对 excel基本的操作如下: # -*- coding: utf-8 -*- import xlrd import xlwt from datetime import date,dat ...

  8. Python-利用xlrd模块操作excel

    在工作中,无论是数据分析,还是批量导入数据,都会去操作excel,当然,数据分析有数据分析的方法,而我在开发中涉及到的是批量导入excel中的数据, 接下来介绍下如何利用python的xlrd模块来读 ...

  9. python xlrd,xlwt 读写excel文件

    python 读excel文件,需要xlrd库.下载地址:https://pypi.python.org/pypi/xlrd python 写excel文件,需要xlwt库.下载地址:https:// ...

随机推荐

  1. Swift基础--ijkplayer编译

    这篇与Swift基本上没有什么关系,只是之后会用到,这次编译是使用Mac编译IOS版本号的.以下是编译流程,最后是整个项目的下载地址.由于下载太慢了. 1.下载源代码 git clone https: ...

  2. poj 3246 Balanced Lineup(线段树)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 38942   Accepted: 18247 ...

  3. Visual Studio中“后期生成事件命令行” 中使用XCopy命令

    将程序所依赖的动态库与其他依赖文件做了分类,使用XCopy命令自动生成相应的目录结构. set source="$(TargetDir)" set output="$(S ...

  4. jquery datagrid设置pageSize不起作用

    http://www.2cto.com/kf/201212/178098.html —————————————————————————————————————————————————————————— ...

  5. 【PM面试题】设计一个股价推送工具

    这一轮面试时间比较短,问题在短时间内不能很全面展开,因此抓住一些关键点变得尤其重要,这里我记录下当时是怎么想这个问题的. 问题解析 子问题1:推送什么?从问题中看出我们需要推送的是股价,用户可以自定义 ...

  6. 关于TextView的一些初步解说

    Android里面的textview是一个相当重要的类.相信做安卓的开发人员在每一个应用里面都一定用到了它,而且它也是Button,EditTextView等子控件的父类. 对于View的流程:mea ...

  7. org.springframework.beans.factory.parsing.BeanDefinitionParsingException

    今天在练习spring aop时.调试程序出现下面错误 org.springframework.beans.factory.parsing.BeanDefinitionParsingException ...

  8. 【ARDUINO】串口无法打开问题

    1.查看是否串口被锁 sudo arduino ls /var/lock sudo rm /var/lock/LCK..ttyACM* 2.查看arduino安装位置 dpkg -S XXXX 3.原 ...

  9. AVA 8 :从永久区(PermGen)到元空间(Metaspace)

    你注意到了吗?JDK 8早期可访问版本已经提供下载了,java 开发人员可以使用java 8 提供的新的语言和运行特性来做一些实验.其中一个特性就是完全的移除永久代(Permanent Generat ...

  10. c++ new(不断跟新)

    1.基础知识 /* 可以定义大小是0的数组,但不能引用,因为没有指向任何对象 new string[10]调用类的默认构造函数 new int[10]没有初始化,但new int[10]()会将数组初 ...