__author__ = "xiaoyan.qin"
#from excel import walkFile, DFS_file_search
'''
Excel文件转csv文件脚本
支持文件夹嵌套
支持xlsx 和 xls 格式
在同级目录下生成对应sheet名称 的文件,采用UTF-8编码
'''
import xlrd
import csv
import os
import sys
# 生成的csv文件名
def DFS_file_search(dict_name):
import os
# list.pop() list.append()这两个方法就可以实现栈维护功能
stack = []
result_txt = []
print(dict_name)
stack.append(dict_name)
while len(stack) != 0: # 栈空代表所有目录均已完成访问
temp_name = stack.pop()
try:
temp_name2 = os.listdir(temp_name) # list ["","",...]
print(temp_name2)
for eve in temp_name2:
stack.append(temp_name + "\\" + eve) # 维持绝对路径的表达
except NotADirectoryError:
result_txt.append(temp_name)
return result_txt
# csv_file_name = 'D:\\excel\\1.csv'
def get_excel_list():
# 获取Excel文件列表
excel_file_list = []
# os.chdir("D:\excel")
os.chdir(path=sys.argv[1])
file_list = []
file_list2 = DFS_file_search(os.getcwd())
for r in file_list2:
file_list.append(r)
for file_name in file_list:
if file_name.endswith('xlsx') or file_name.endswith('xls'):
excel_file_list.append(file_name)
return excel_file_list
def get_excel_header(excel_name_for_header):
# 获取表头,并将表头全部变为小写
workbook = xlrd.open_workbook(excel_name_for_header)
table = workbook.sheet_by_index(0)
# row_value = table.row_values(0)
row_value = [i.lower() for i in table.row_values(0)]
return row_value
def read_excel(excel_name):
# 读取Excel文件每一行内容到一个列表中
workbook = xlrd.open_workbook(excel_name)
# 读取第一个sheet
sheets = workbook.sheet_names()
num = workbook.nsheets
for i in range(num):
sheetname = workbook.sheets()[i]
nrows = sheetname.nrows
ncols = sheetname.ncols
# 跳过表头,从第一行数据开始读
for rows_read in range(0, nrows):
# 每行的所有单元格内容组成一个列表
row_value = []
for cols_read in range(ncols):
# 获取单元格数据类型
ctype = sheetname.cell(rows_read, cols_read).ctype
# 获取单元格数据
nu_str = sheetname.cell(rows_read, cols_read).value
# 判断返回类型
# 0 empty,1 string, 2 number(都是浮点), 3 date, 4 boolean, 5 error
# 是2(浮点数)的要改为int
if ctype == 2:
nu_str = int(nu_str)
row_value.append(nu_str)
yield row_value, sheetname.name #返回sheet表格及sheet名称
def xlsx_to_csv(csv_file_name, row_value):
# 生成csv文件
with open(csv_file_name, 'a', encoding='utf-8', newline='') as f: # newline=''不加会多空行
write = csv.writer(f)
write.writerow(row_value)
# def read_sheet_name(excel_name):
# workbook = xlrd.open_workbook(excel_name)
# # 读取第一个sheet
# num = workbook.nsheets
# for i in range(num):
# sheets = workbook.sheets()[i]
# print("sheetname " + sheets.name)
# yield sheets.name
if __name__ == '__main__':
# dirnam = sys.argv[2]
# csv_file_name = dirnam + sys.argv[3]
# print(csv_file_name)
# 获取Excel列表
excel_list = get_excel_list()
print("excle", excel_list)
# 获取Excel表头并生成csv文件标题
#xlsx_to_csv(csv_file_name, get_excel_header(excel_list[0]))
# 生成csv数据内容
for excel_name in excel_list:
for (row_value, sheetname) in read_excel(excel_name):
xlsx_to_csv(sheetname, row_value)
print('Excel文件转csv文件结束 ')