excel合并单元格数据读取

摘要:
importxlrdapply_dic=[]defget_excel(excel_path):withxlrd.open_workbook(excel/path)asworkbook:name_sheets=workbook.sheet_Names()#获取excel的工作表列表,并将其存储为indexinname_sheets:#for循环读取每个工作表的内容
import xlrd
apply_dic = []
def get_excel(excel_path):
with xlrd.open_workbook(excel_path) as workbook :
name_sheets = workbook.sheet_names() #获取Excel的sheet表列表,存储是sheet表名
for index in name_sheets: #for 循环读取每一个sheet表的内容
sheet_info = workbook.sheet_by_name(index) #根据表名获取表中的所有内容,sheet_info也是列表,列表中的值是每个单元格里值
first_line = sheet_info.row_values(0) #获取首行,我这里的首行是表头,我打算用表头作为字典的key,每一行数据对应表头的value,每一行组成一个字典
values_merge_cell = merge_cell(sheet_info) #这里是调用处理合并单元格的函数
print(values_merge_cell)
for i in range(1, sheet_info.nrows): #开始为组成字典准备数据
other_line = sheet_info.row_values(i)
for key in values_merge_cell.keys():
if key[0] == i:
other_line[key[1]] = values_merge_cell[key]
#print(other_line)
dic = list_dic(first_line,other_line) #调用组合字典的函数,传入key和value,字典生成
apply_dic.append(dic)
return apply_dic

def list_dic(list1,list2):
'''
two lists merge a dict,a list as key,other list as value
:param list1:key
:param list2:value
:return:dict
'''
dic = dict(map(lambda x,y:[x,y], list1,list2))
return dic

def merge_cell(sheet_info):
'''
#handle Merge transverse cells and handle Merge Vertical Cells, assign empty cells,
:param rlow:row, include row exclusive of row_range
:param rhigh:row_range
:param clow:col, include col exclusive of col_range
:param chigh:col_range
:param sheet_info:object of sheet
:return:dic contain all of empty cells value
'''
merge = {}
merge_cells = sheet_info.merged_cells
for (rlow, rhigh, clow, chigh) in merge_cells:
value_mg_cell = sheet_info.cell_value(rlow, clow)
if rhigh-rlow == 1:
# Merge transverse cells
for n in range(chigh-clow-1):
merge[(rlow, clow+n+1)] = value_mg_cell
elif chigh-clow == 1:
# Merge Vertical Cells
for n in range(rhigh-rlow-1):
merge[(rlow+n+1, clow)] = value_mg_cell
return merge

if __name__ == '__main__':
excel_path=r'E:ReviewTRfile.xlsx'
print(get_excel(excel_path))

免责声明:文章转载自《excel合并单元格数据读取》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇理论+实例,带你掌握Linux的页目录和页表OPCDAAuto.dll的C#使用方法浅析下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Office办公软件操作技巧 office快捷键大全

››››F键 F1:显示「帮助」任务窗格。 F2+shift:添加或编辑单元格批注 F3+shift:显示「插入函数」对话框 F4:重复上一个命令或操作 F5+G:显示「定位」对话框 F9+ctrl:最小化窗口 F10:打开或关闭窗口 ››››ctrl键 ctrl+1:显示「设置单元格格式」 ctrl+2:应用或取消加粗格式 ctrl+3:应用或取消倾斜格...

Java学习---Excel读写操作

1.1.1. 简介 Apache POI 使用Apache POI 完成Excel读写操作 Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能。 http://poi.apache.org/ 下载POI的jar包 ,包括以下...

GJM:C# WinForm开发系列

1.DataGridView实现课程表testcontrol.rar 2.DataGridView二维表头及单元格合并DataGridView单元格合并和二维表头.rarmyMultiColHeaderDgv.rar 3.DataGridView单元格显示GIF图片gifanimationindatagrid.rar 4.自定义显示DataGrid...

Jxl操作Excel设置背景、字体颜色、对齐方式、列的宽度(转)

原文出处:http://zhouhaitao.iteye.com/blog/1842769 最近项目中需要用到导出Excel文件,以下是我写了一个通过jxl操作Excel的例子,熟悉Jxl的使用。 有一个比较难以处理的问题就是自动适应文本宽度的问题。 以下我也在网上找了一下 :有如下的方式处理: CellView cellView = new CellVi...

java生成Excel(JXL类库)

jxl是java操作excel的工具, 在开源世界中,有两套比较有影响的API可 供使用,一个是POI,一个是jExcelAPI。其中功能相对POI比较弱一点。但jExcelAPI对中文支持非常好,API是纯Java的, 并不 依赖Windows系统,即使运行在Linux下,它同样能够正确的处理Excel文件。 另外需要说明的是,这套API对图形和图表的支...

Excel随机生成批量日期,以及注意事项

这个是WPS里写的一个函数,用来随机生成日期。首先E1和E2是两个日期端点,右键把单元格格式先设置成“日期”中的“xxxx年xx月xx日 xx:xx”,然后E3=E1-E2算出它们的距离。 在E4里面,写如图的函数=$E$1-RANBETWEEN(0,$E$3),直接翻译过来就是,E4单元格的值等于E1的值-(0到E3之间的随机值)。这里之所以不直接用E...