# coding=utf-8
import re
from openpyxl import load_workbook
from openpyxl import Workbook    # 保存为excel表格
from openpyxl.workbook.defined_name import DefinedName
# from openpyxl.utils import absolute_coordinate, quote_sheetnameclass Excel():def __init__(self):self.wb = Workbook()  # 第一次创建新 excel文件时用# self.ws = self.wb.active# self.wb = load_workbook('C:\\Users\\Administrator\\Desktop\\all_spider_excel_05\\new_excel\\_excel_data_new.xlsx')    # 已有excel文件,往里添加表时用def save_excel(self):self.wb.save('C:\\Users\\Administrator\\Desktop\\all_spider_excel_05\\new_excel\\_excel_data_new.xlsx')def read_excel(self, filepath, name):file_name = filepath + name + '.xlsx'wb = load_workbook(file_name)# ws = wb.active    # # 等同于  ws = wb.get_active_sheet() #通过_active_sheet_index设定读取的表,默认0读第一个表sheet_names = wb.sheetnames  # ----- ['uv_付费', 'uv_定费用', 'Sheet']print('-----', sheet_names)for k in range(len(sheet_names)):ws = wb[sheet_names[k]]  # index为0为第一张表name = sheet_names[k]print(name , '---', k)# 创建新表写入数据self.ws = self.wb.create_sheet("%s" % name, k)  # 插入到最开始的位置# 读取原表数据l_num = ws.max_column  # 最大列h_num = ws.max_row  # 最大行for i in range(1, h_num + 1):  # h_num + 1line_data = []for j in range(1, l_num + 1):lie_title = ws.cell(row=i, column=j)  # 每列标题  <Cell '固定费用'.A1># print(lie_title)a_value = lie_title.value  # 此单元格里边的值coord = lie_title.coordinate  # 此单元格坐标  A1  B1# print(aa, type(aa))  # class= str# print(coord, type(coord))  # class= strline_data.append(a_value)print(line_data)# 写入新表中self.ws.append(line_data)# 写入新表中self.save_excel()# 调用 添加定义名称self.add_name()def add_name(self):    #####  添加定义名称xl_file = 'C:\\Users\\Administrator\\Desktop\\all_spider_excel_05\\new_excel\\_excel_data_new.xlsx'wb = load_workbook(xl_file)# ws = wb.active    # # 等同于  ws = wb.get_active_sheet() #通过_active_sheet_index设定读取的表,默认0读第一个表sheet_names = wb.sheetnames# print('-----', sheet_name)for i in range(len(sheet_names)):wb = load_workbook(xl_file)# sheet_names = wb.sheetnamesw_name = sheet_names[i]ws = wb[w_name]  #l_num = ws.max_column  # 最大列h_num = ws.max_row  # 最大行# print(ws.max_row)# print(ws.max_column)a_list = []all_lie_list = []# wb = Workbook()for j in range(1, l_num + 1):  # 列lie_list = []# for i in range(1, h_num + 1):  # 行title = ws.cell(row=1, column=j)  # 每列标题name = title.value# print(j)a = ws.cell(row=1, column=j)coord = a.coordinate  # 此单元格坐标  A1  B1# print(coord)l = re.findall(r'^[A-Z]*', coord)[0]h = re.findall(r'[0-9]*$', coord)[0]lie_list.append([coord, name])all_lie_list.append([l, name])  # 添加  列名(A,B),  每一列第一行的值print(all_lie_list)  # [['A', 'SKU'], ['B', 'XL'], ['C', 'freeUV'], ['D', 'changeo'], ['E', 'changeAV'], ['F', 'callchangeo'], ['G', 'callchangeAV'], ['H', 'silencechangeo'], ['I', 'silencechangeAV'], ['J', 'buyingprice']]for lie in all_lie_list:bb = '%s!$' % w_name + lie[0] + '$1:$' + lie[0] + '$' + str(h_num)dname = DefinedName(name='%s' % lie[1])dname.value = bbwb.defined_names.append(dname)wb.save('C:\\Users\\Administrator\\Desktop\\all_spider_excel_05\\change_excel\\%s_01.xlsx' % w_name)    # 'C:\\Users\\Administrator\\Desktop\\all_spider_excel_05\\change_excel\\%s_01.xlsx' % w_namewb.close()if __name__ == '__main__':import osfilepath = "C:\\Users\\Administrator\\Desktop\\all_spider_excel_05\\read_excel\\"pathDir = os.listdir(filepath)excel = Excel()for alldir in pathDir:name = alldir.replace('.xlsx', '')print(name)# a = input('输入excel表格原名 :')# name = 'zidongduqu'# f_name = "C:\\Users\\Administrator\\Desktop\\spider_excel\\excel_originally\\_excel_数据"# xl_savefile = "C:\\Users\\Administrator\\Desktop\\spider_excel\\excel_new\\_excel_数据" + ".xlsx"  # 新名  excel_add_name# excel.read_excel(filepath, name)excel.add_name()

python excel 读写,定义名称相关推荐

  1. EXCEL中定义名称

    名称的定义是EXCEL的一基础的技能,可是,如果你掌握了,它将给你带来非常实惠的妙处! 一.定义名称的妙处 1. 如何定义名称 插入 – 名称 – 定义(或者按<Ctrl+F3>组合键), ...

  2. python excel sheet个数_python得到一个excel的全部sheet标签值方法

    python读取excel莫个页签sheets()行数,并且获取里在线分享等,哪位好心人士对于python操作excel熟练请帮忙解 不要在表达爱时那么无力,却又在表达伤害时如此锋利. 如何返使用xl ...

  3. python怎么读excelsheet_python3 excle(python怎么读写excel文件)

    python读取已经打开的3个word和excle文件的路径 用 win32com 操控 word和Excel就可以实现 咋样把python写入excle中 # 需安装 xlrd-0.9.2 和 xl ...

  4. python怎么读写文件-python3 excle(python怎么读写excel文件)

    python读取已经打开的3个word和excle文件的路径 用 win32com 操控 word和Excel就可以实现 咋样把python写入excle中 # 需安装 xlrd-0.9.2 和 xl ...

  5. python怎么读excel文件-python如何读写excel文件

    python读写excel的方式有很多,不同的模块在读写的方法上稍有区别: 用xlrd和xlwt进行excel读写: 用openpyxl进行excel读写: 用pandas进行excel读写: 为了方 ...

  6. python中读写excel_python读写Excel

    Python读写excel 对于python 处理excel 表格,这个日常工作的需求还是不少,于是查查资料,自己写写,总结一下,记录一下,下次遇到直接拿过来用,方便自己,方便大家. 好在python ...

  7. 【Python 基础教程 20】全面掌握Python3输入输出:从入门到高级的实用指南(文件读写,Excel读写,Markdowm文件读写)

    目录标题 1. 简介 1.1 Python3 输入输出的定义 1.2 输入输出的重要性和常见应用场景 2. 标准输入输出 2.1 介绍标准输入(stdin) 2.2 介绍标准输出(stdout) 2. ...

  8. python读取数据文件-python多种读写excel等数据文件的方式(收藏篇)

    前言: python处理数据文件的途径有很多种,可以操作的文件类型主要包括文本文件(csv.txt.json等).excel文件.数据库文件.api等其他数据文件.下面小编整理下python到底有哪些 ...

  9. 创建到另一个工作簿中已定义名称的外部引用的帮助(Excel)

    创建到另一个工作簿中已定义名称的外部引用的帮助(Excel) 打开将包含外部引用的工作簿(目标工作簿)和包含要链接到的数据的工作簿(源工作簿). 在源工作簿中,单击快速访问工具栏上的"保存& ...

最新文章

  1. mergesort java_归并排序详解(MergeSort)递归和非递归实现
  2. 技术新进展!谷歌AI部门宣布发现新技术以加速AI神经网络训练
  3. redis 缓存过期默认时间_Redis 过期时间与内存管理
  4. react native中一次错误排查 Error:Error: Duplicate resources
  5. asp:HyperLink中 Eval要用string.Format绑定
  6. css入门教程资料(5)
  7. python3-numpy IO load()、save()、savez()、loadtxt()、savetxt()、tofile()、fromfile()
  8. 六招教你快速提升网站交互体验,降低跳出率
  9. python中关于正则表达式二
  10. 医疗搜索中的query词权重算法探索
  11. 服务端如何防止订单重复支付?
  12. defunct 进程占用端口_纯干货:23个服务器常见问题处理方法!
  13. 退货地址导出(拼多多)
  14. 原型工具axure7.0
  15. 【EasyExcel导入、导出(百万数据量测试)粘贴即用】
  16. 编写Java脚本统计工程代码总行数
  17. cutelyst教程_02 _Cutelyst基础
  18. c#怎么设置winform中dgv的表头边框线
  19. iOS自动打包,并上传蒲公英
  20. 第16届重型车辆运输技术国际大会(HVTT16) 2021中国国际道路运输装备科技博览会(RTET)

热门文章

  1. adsl网站服务器能行不,ADSL用户个人网站服务器创建办法.docx
  2. Appium连接逍遥模拟器,解决Timing Out
  3. 苹果真伪查询_苹果自研M1芯片Mac电脑开启预售!该不该冲?
  4. 自学Python去接单,一周就能赚取近1200块,你看不起的行业,真的很赚钱!
  5. java枚举值转化中文_多个枚举的国际化(枚举值的转换)
  6. 全网最透彻的Hash分析
  7. 计算机无法搜索到打印机驱动,电脑连接打印机需要装什么驱动(电脑搜不到打印机设备)...
  8. 计算机双工模式,windows10系统如何设置网络双工模式?
  9. android 服务开机启动慢,Android App启动慢原因
  10. pip安装的依赖不在虚拟环境中