#!/usr/bin/python
# -*- coding:utf-8 -*-
#python 2.7
from openpyxl import load_workbook
import csv
import os,sys
reload(sys)
sys.setdefaultencoding('utf8')
def xlsx2csv(filename):
try:
xlsx_file_reader = load_workbook(filename=filename)
for sheet in xlsx_file_reader.get_sheet_names():
# 每个sheet输出到一个csv文件中,文件名用xlsx文件名和sheet名用'_'连接
csv_filename = '{xlsx}_{sheet}.csv'.format(
xlsx=os.path.splitext(filename.replace(' ', '_'))[0],
sheet=sheet.replace(' ', '_'))
#生成文件将xlsx文件内容写入到csv中
csv_file = file(csv_filename, 'wb')
csv_file_writer = csv.writer(csv_file)
sheet_ranges = xlsx_file_reader[sheet]
for row in sheet_ranges.rows:
row_container = []
for cell in row:
if type(cell.value) == unicode:
row_container.append(cell.value.encode('utf-8'))
else:
row_container.append(str(cell.value))
csv_file_writer.writerow(row_container)
csv_file.close()
except Exception as e:
print e
if __name__ == '__main__':
#如脚本后没有跟xlsx的文件名将不会执行
if len(sys.argv) != 2:
print 'usage: xlsx2csv <xlsx file name>'
else:
xlsx2csv(sys.argv[1])
sys.exit(0)
xlsx转换成csv格式
#!/usr/bin/python#-*-coding:utf-8-*-#python2.7fromopenpyxlimportload_workbookimportcsvimportos,sysreload(sys)sys.setdefaultenco...
·
转载于:https://my.oschina.net/FrankXin/blog/596041
更多推荐
已为社区贡献1条内容
所有评论(0)