python学习01–批量将excel表名和首行字段名改为小写
在操作postgresql数据库时,普通的关系表可以通过Navicat录入postgresql数据库中,但是postgresql数据库对大小写敏感,为了工程方便,所有表名和字段名统一为小写,为了入库方便,利用python修改excel文件名,以及首行代表的字段名
# -*- coding: UTF-8 -*-
import os
import openpyxl
# 获取当前目录中的所有文件
def file_name(file_dir):
File_Name = []
for files in os.listdir(file_dir):
if os.path.splitext(files)[1] == '.xlsx':
File_Name.append(files)
return File_Name
# 处理excel
def dealExcel(path):
data = openpyxl.load_workbook(path) # 可读可写
#遍历所有的sheet
for sheet in data:
# 把第一行的值全部换成小写字母
#获取列数
sheet.title=str(sheet.title).lower()
columCount=sheet.max_column
rowCount=sheet.max_row
if(rowCount==0):
continue
if(columCount==0):
continue
else:
for i in range(0,columCount):
b3 = sheet.cell(1,i+1)
valueModify=str(b3.value).lower()
b3.value=str(b3.value).lower()
data.close()
os.remove(path)
data.save(path.lower())
currentDir = os.getcwd()
txt_file_name = file_name(currentDir)
for filePath in txt_file_name:
path = currentDir + "/" + filePath
print("正在处理: ",format(path))
dealExcel(path)
print("处理成功!")
这里处理的是同级目录下的所有*.xlsx