python实现Execl转csv方法收集

1.使用xlrd
# -*- coding: utf-8 -*-
import xlrd
import csv
from os import sys


def csv_from_excel(excel_file):
    workbook = xlrd.open_workbook(excel_file)
    all_worksheets = workbook.sheet_names()
    for worksheet_name in all_worksheets:
        worksheet = workbook.sheet_by_name(worksheet_name)
        your_csv_file = open(''.join([worksheet_name,'.csv']), 'wb')
        wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)


        for rownum in xrange(worksheet.nrows):
            wr.writerow([unicode(entry).encode("utf-8") for entry in worksheet.row_values(rownum)])
        your_csv_file.close()


if __name__ == "__main__":
    csv_from_excel(sys.argv[1])

===============================
# -*- 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)
===============================
2. 使用pandas


import pandas as pd
data_xls = pd.read_excel('your_workbook.xls', 'Sheet1', index_col=None)
data_xls.to_csv('your_csv.csv', encoding='utf-8')


安装pandas,看实际情况使用:
安装numpy:
pip install numpy
安装pandas:
 https://pypi.python.org/pypi/pandas
安装过程遇到报错找不到Python:http://blog.csdn.net/zdnlp/article/details/12171687
安装dateutil:
https://pypi.python.org/pypi/python-dateutil
测试:
import pandas




3. csvkit

你可能感兴趣的:(编码,python,文本)