python腳本實(shí)現(xiàn)xls(xlsx)轉(zhuǎn)成csv
來源:懂視網(wǎng)
責(zé)編:小采
時間:2020-11-27 14:35:43
python腳本實(shí)現(xiàn)xls(xlsx)轉(zhuǎn)成csv
python腳本實(shí)現(xiàn)xls(xlsx)轉(zhuǎn)成csv:# xls_csv 把xls,xlsx格式的文檔轉(zhuǎn)換成csv格式 # 使用 python xls2csv.py # -*- coding: utf-8 -*- import xlrd import xlwt import sys from datetime import date,datetime def read_excel(filen
導(dǎo)讀python腳本實(shí)現(xiàn)xls(xlsx)轉(zhuǎn)成csv:# xls_csv 把xls,xlsx格式的文檔轉(zhuǎn)換成csv格式 # 使用 python xls2csv.py # -*- coding: utf-8 -*- import xlrd import xlwt import sys from datetime import date,datetime def read_excel(filen
# xls_csv
把xls,xlsx格式的文檔轉(zhuǎn)換成csv格式
# 使用
python xls2csv.py
# -*- coding: utf-8 -*-
import xlrd
import xlwt
import sys
from datetime import date,datetime
def read_excel(filename):
workbook = xlrd.open_workbook(filename)
# print sheet2.name,sheet2.nrows,sheet2.ncols
sheet2 = workbook.sheet_by_index(0)
for row in xrange(0, sheet2.nrows):
rows = sheet2.row_values(row)
def _tostr(cell):
if type(u'') == type(cell):
return ""%s"" % cell.encode('utf8')
else:
return ""%s"" % str(cell)
print ','.join([_tostr(cell) for cell in rows ])
if __name__ == '__main__':
filename = sys.argv[1]
read_excel(filename)
再給大家分享一則代碼
xlsx文件解析處理:openpyxl庫 csv文件格式生成:csv
python#coding: utf-8
# 依賴openpyxl庫:http://openpyxl.readthedocs.org/en/latest/
from openpyxl import Workbook
from openpyxl.compat import range
from openpyxl.cell import get_column_letter
from openpyxl import load_workbook
import csv
import os
import sys
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(' ', '_'))
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__':
if len(sys.argv) != 2:
print('usage: xlsx2csv
')
else:
xlsx2csv(sys.argv[1])
sys.exit(0)
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com
python腳本實(shí)現(xiàn)xls(xlsx)轉(zhuǎn)成csv
python腳本實(shí)現(xiàn)xls(xlsx)轉(zhuǎn)成csv:# xls_csv 把xls,xlsx格式的文檔轉(zhuǎn)換成csv格式 # 使用 python xls2csv.py # -*- coding: utf-8 -*- import xlrd import xlwt import sys from datetime import date,datetime def read_excel(filen