1.读取关联表格代码(再import openpyxl库的前提下)#######################################################
#coding=utf-8
import os
import random
import string
import openpyxl
from openpyxl import *
from openpyxl.reader.excel import load_workbook
from openpyxl.compat import range
from openpyxl.cell.read_only import EMPTY_CELL
import win32com.client
from openpyxl import Workbook
from openpyxl.utils import get_column_letter
from openpyxl.utils import column_index_from_string
import sys
reload(sys)
sys.setdefaultencoding( "GBK" )
def Rir_sheet(wb,sheet_name,rc):sheet = wb[sheet_name]sheetdata = wb[sheet_name]url = str(rc)chrvalue = ""
    for i in url:if "a" <= i and i <= "z" or i >= 'A' and i <= 'Z':chrvalue = chrvalue + icellvalue = sheetdata.cell(row=int(url.strip(chrvalue)), column=column_index_from_string(chrvalue)).value.decode('GBK')if "!" in cellvalue and cellvalue[0] == "=":sheetvalue = cellvalue.split("!")[0].strip("=")return Rir_sheet(wb, sheetvalue, cellvalue.split("!")[1])elif "!" not in cellvalue and cellvalue[0] == "=":return Rir_cell(wb, sheet_name, cellvalue.strip('='))else:return cellvalue
def Rir_cell(wb,sheet_name,rc):ws=wb[sheet_name]url = str(rc)chrvalue = ""
    for i in url:if "a" <= i and i <= "z" or i >= 'A' and i <= 'Z':chrvalue = chrvalue + icellvalue = ws.cell(row=int(url.strip(chrvalue)), column=column_index_from_string(chrvalue)).value.decode('GBK')if "!" in cellvalue and cellvalue[0] == "=":sheetvalue = cellvalue.split("!")[0].strip("=")return Rir_sheet(wb, sheetvalue, cellvalue.split("!")[1])elif "!" not in cellvalue and cellvalue[0] == "=":return Rir_cell(wb,sheet_name,cellvalue.strip('='))else:return cellvalue
if __name__ == '__main__':wb = openpyxl.load_workbook(u'C://Users/Administrator//Desktop//测试文档.xlsx')print Rir_cell(wb,'Sheet2',"D2")

#######################################################
2.实际项目中的代码如下
#######################################################

#coding=utf-8
import os
import random
import string
import openpyxl
from openpyxl.reader.excel import load_workbook
from openpyxl.compat import range
from openpyxl.cell.read_only import EMPTY_CELL
from openpyxl.utils import column_index_from_string
import win32com.client
import sys
reload(sys)
sys.setdefaultencoding( "UTF-8" )
#获取Excel中数据
# 返回值形如:
# =[{A1:A2,B1:B2,C3:C2,...},{A1:A3,B1:B3,C3:C3,...},{A1:A4,B1:B4,C3:C4,...},....]
def GetSheetData( file_path, sheet_name):wb = openpyxl.load_workbook(file_path)# 获取workbook中所有的表格
    sheets = wb.sheetnames# 按照指定sheet_name去查询
    sheet = wb[sheet_name]# 统计第一行中字段多少,一遍后面使用
    column_names = []for r in range(1, sheet.max_column + 1):column_names.append(str(sheet.cell(row=1, column=r).value))column_num = len(column_names)list = []if sheet_name in sheets:sheet = wb[sheet_name]for r in range(2, sheet.max_row + 1):dict = {}for c in range(1, sheet.max_column + 1):if str(sheet.cell(row=r, column=c).value) == "None":dict[column_names[c - 1].decode('UTF-8')] = ""
                else:cellvalue = str(sheet.cell(row=r, column=c).value).decode('UTF-8')if "!" in cellvalue and cellvalue[0] == "=":  # 判断数据是否是关联不同sheet页单元格数据,是就是下面方式处理
                        sheetvalue = cellvalue.split("!")[0].strip("=")if sheetvalue in sheets:  # 判断获取的sheet页是否在本Excel中是否存字,存在就调用Rir_sheet(wb,sheet_name,rc)方法
                            dict[column_names[c - 1].decode('UTF-8')] = Rir_sheet(wb, sheetvalue,cellvalue.split("!")[1])else:dict[column_names[c - 1].decode('UTF-8')] = cellvalueelif "!" not in cellvalue and cellvalue[0] == "=":  # 判断数据是否是关联不同sheet页单元格数据,是就是下面方式处理
                        dict[column_names[c - 1].decode('UTF-8')] = Rir_cell(wb, sheet_name, cellvalue.strip('='))else:  # 如果无关联就直接存字
                        dict[column_names[c - 1].decode('UTF-8')] = cellvaluenum = 0
                for i in dict.values():if len(i.replace(" ", '')) == 0:num = num + 1
                if num != column_num:list.append(dict)return listelse:print 'your input not in sheets'

#######同一个Excel中,不同sheet页的单元格之间的关联值获取########
def Rir_sheet( wb, sheet_name, rc):sheet = wb[sheet_name]sheetdata = wb[sheet_name]url = str(rc)chrvalue = ""
    for i in url:if "a" <= i and i <= "z" or i >= 'A' and i <= 'Z':chrvalue = chrvalue + icellvalue = sheetdata.cell(row=int(url.strip(chrvalue)), column=column_index_from_string(chrvalue)).value.decode('UTF-8')if "!" in cellvalue and cellvalue[0] == "=":sheetvalue = cellvalue.split("!")[0].strip("=")return Rir_sheet(wb, sheetvalue, cellvalue.split("!")[1])elif "!" not in cellvalue and cellvalue[0] == "=":return Rir_cell(wb, sheet_name, cellvalue.strip('='))else:return cellvalue#######同一个Excel中,相同sheet页的单元格之间的关联值获取########
def Rir_cell( wb, sheet_name, rc):ws = wb[sheet_name]url = str(rc)chrvalue = ""
    for i in url:if "a" <= i and i <= "z" or i >= 'A' and i <= 'Z':chrvalue = chrvalue + icellvalue = ws.cell(row=int(url.strip(chrvalue)), column=column_index_from_string(chrvalue)).value.decode('UTF-8')if "!" in cellvalue and cellvalue[0] == "=":sheetvalue = cellvalue.split("!")[0].strip("=")return Rir_sheet(wb, sheetvalue, cellvalue.split("!")[1])elif "!" not in cellvalue and cellvalue[0] == "=":return Rir_cell(wb, sheet_name, cellvalue.strip('='))else:return cellvalue
if __name__ == '__main__':a=GetSheetData(u'C://Users/Administrator//Desktop//测试文档.xlsx','Sheet2')print a[0]['colum6']print a[0]['colum2']print a[0]['colum3']print a[0]['colum4']

############################################
转载请注明出处
###########################################
跨Excel的关联值获取,后续空了补上
###########################################

python读取Excel中关联表格的数据(只要是同Excel中)---可以解决无限次同一个Excel中跨sheet或同sheet中表格关联--相关推荐

  1. python读取Excel中关联表格的数据(只要是同Excel中

    来自CSDN-Mr熊 https://blog.csdn.net/qq_41030861/article/details/80515984 谁能告诉我前面这一堆都是什么啊... python读取Exc ...

  2. python读取文件某一行-使用python读取.text文件特定行的数据方法

    如何用python循环读取下面.txt文件中,用红括号标出来的数据呢? 首先,观察数据可知,不同行的第一个数据元素不一样,所以考虑直接用正则表达式. 再加上,对读和写文件的操作,就行了 注:我用的是p ...

  3. python读取文件多行内容-使用python读取.text文件特定行的数据方法

    如何用python循环读取下面.txt文件中,用红括号标出来的数据呢? 首先,观察数据可知,不同行的第一个数据元素不一样,所以考虑直接用正则表达式. 再加上,对读和写文件的操作,就行了 注:我用的是p ...

  4. 数组x中数据复制到数组y中,重复的数据只存储一次,最后输出y;计算x中数据的平均值ave及大于平均值的元素个数n。c++实现

    题目描述 编程序,实现如下功能: (1)定义两个一维数组x,y,不超过50个元素. (2)从键盘输入k个整数到数组x中. (3)计算x中数据的平均值ave及大于平均值的元素个数n并输出. (4)将数组 ...

  5. ppt图片在word中不能正常显示,只显示为矩形框的解决方法

    ppt图片在word中不能正常显示,只显示为矩形框的解决方法 参考文章: (1)ppt图片在word中不能正常显示,只显示为矩形框的解决方法 (2)https://www.cnblogs.com/ga ...

  6. python读取成功_Python如何从文件读取数据()

    Python编写一个文件读写程序(命令行程序) def readfromfile(filename): with open(filename, 'rt') as handle: return hand ...

  7. python 读取json转为docx_python 将json数据提取转化为txt的方法

    python3 如何把一个txt文件转换成json数据,txt包含前言 今天拿到一个传感器信号文件,txt格式,有十几行,每行是一组json格式数据,有两个字段(键):'series'和'id',共4 ...

  8. python逐行读取json_如何用python读取json文件里指定的数据

    JSON文件who.json内容如下:{"name":"mss","version":"1.0.0","des ...

  9. speedoffice表格中怎么删除重复数据只保留一个

    工作中,在录入数据时,会出现录入重复的情况,如何找出重复的数据尤为重要.今天一起来学习一下excel怎么删除重复项只保留一个吧 1.首先选中数据区域,单击"数据"选项卡下方&quo ...

最新文章

  1. leetcode算法题--圆圈中最后剩下的数字
  2. 在ppt中插入excel 或者word 文档
  3. js实现上传图片及时预览
  4. gpedit msc组策略面板 win10在哪里_Win10系统gpedit.msc在哪?Win10系统gpedit.msc组策略打不开怎么办?...
  5. pc-H5 适配方案
  6. Atitit webclient httpclient技术总结 RestTemplate Atitit CateIT重要技术httpclient iduah2 impt 体系树路径:CS
  7. xml突然变成空白_真实职场故事:开会时候,被领导突然提问,我差点被开除了...
  8. 【数学建模】看了都说好,高温作业专用服装设计拟合函数模型,强烈建议收藏
  9. 微软使用“钞能力”: 687 亿美元收购动视暴雪!
  10. Java实现猜拳游戏
  11. 声艺数字调音台si说明书32路_Soundcraft 声艺 Si Impact 数字调音台 32路数字调音台...
  12. Iproxy USB连接手机
  13. matplotlib 绘制直方图
  14. 【数据结构与算法】试卷 1(含答案)
  15. 液晶显示器画面模糊有毛边解决方法
  16. 微型计算机中常见到的Ega.VGA等是指,大学计算机基础选择题汇总
  17. b和B的区别?大B与小b的区别(Bps与bps)以及b、B、KB、MB、TB、PB、EB的换算
  18. NIH发布包含10600张CT图像数据库 为AI算法测试铺路
  19. 山东大学数字图像处理实验(六)
  20. ShareSDK 微信及其朋友圈集成步骤

热门文章

  1. 完全约束与非完全约束
  2. B 站又上热搜了, HR 称「核心用户都是 Loser」
  3. datagridview取消默认选中_很多玩家到现在都没发现的官方BUG:堪比工具,无法选中也能被控...
  4. 二元函数图像生成器_谷歌程序员自制秃头生成器:一键get张东升同款发型,今天你脱发了吗?...
  5. 新年焕新,强推“60倍超级变焦”的vivo X30 Pro双模5G
  6. c语言字符串非对称加密,RSA算法C语言实现(支持任意位密钥)
  7. 在互联网公司,有这几个迹象,就该提桶跑路了!
  8. Android 短信群发功能的实现
  9. java练习题 = 小汽车 + 计算器 + 人名年龄
  10. 股票量化交易策略之选股、模拟交易过程