2017-08-17 232 views
1

我試圖通過使用此Python腳本將目錄中的CSV文件循環轉換爲XLS,但到目前爲止我無法實現太多。會appreaciate任何幫助。這段代碼的操作說,他一次只編寫一個擴展集(1個文件),但我相信它可以被一些python程序員的經驗所改變。使用python將csv轉換爲xls

#!/usr/local/bin/python 
# Tool to convert CSV files (with configurable delimiter and text wrap 
# character) to Excel spreadsheets. 
import string 
import sys 
import getopt 
import re 
import os 
import os.path 
import csv 
from pyExcelerator import * 

def usage(): 
    """ Display the usage """ 
    print "Usage:" + sys.argv[0] + " [OPTIONS] csvfile" 
    print "OPTIONS:" 
    print "--title|-t: If set, the first line is the title line" 
    print "--lines|-l n: Split output into files of n lines or less each" 
    print "--sep|-s c [def:,] : The character to use for field delimiter" 
    print "--output|o : output file name/pattern" 
    print "--help|h : print this information" 
    sys.exit(2) 

def openExcelSheet(outputFileName): 
    """ Opens a reference to an Excel WorkBook and Worksheet objects """ 
    workbook = Workbook() 
    worksheet = workbook.add_sheet("Sheet 1") 
    return workbook, worksheet 

def writeExcelHeader(worksheet, titleCols): 
    """ Write the header line into the worksheet """ 
    cno = 0 
    for titleCol in titleCols: 
    worksheet.write(0, cno, titleCol) 
    cno = cno + 1 

def writeExcelRow(worksheet, lno, columns): 
    """ Write a non-header row into the worksheet """ 
    cno = 0 
    for column in columns: 
    worksheet.write(lno, cno, column) 
    cno = cno + 1 

def closeExcelSheet(workbook, outputFileName): 
    """ Saves the in-memory WorkBook object into the specified file """ 
    workbook.save(outputFileName) 

def getDefaultOutputFileName(inputFileName): 
    """ Returns the name of the default output file based on the value 
     of the input file. The default output file is always created in 
     the current working directory. This can be overriden using the 
     -o or --output option to explicitly specify an output file """ 
    baseName = os.path.basename(inputFileName) 
    rootName = os.path.splitext(baseName)[0] 
    return string.join([rootName, "xls"], '.') 

def renameOutputFile(outputFileName, fno): 
    """ Renames the output file name by appending the current file number 
     to it """ 
    dirName, baseName = os.path.split(outputFileName) 
    rootName, extName = os.path.splitext(baseName) 
    backupFileBaseName = string.join([string.join([rootName, str(fno)], '-'), extName], '') 
    backupFileName = os.path.join(dirName, backupFileBaseName) 
    try: 
    os.rename(outputFileName, backupFileName) 
    except OSError: 
    print "Error renaming output file:", outputFileName, "to", backupFileName, "...aborting" 
    sys.exit(-1) 

def validateOpts(opts): 
    """ Returns option values specified, or the default if none """ 
    titlePresent = False 
    linesPerFile = -1 
    outputFileName = "" 
    sepChar = "," 
    for option, argval in opts: 
    if (option in ("-t", "--title")): 
     titlePresent = True 
    if (option in ("-l", "--lines")): 
     linesPerFile = int(argval) 
    if (option in ("-s", "--sep")): 
     sepChar = argval 
    if (option in ("-o", "--output")): 
     outputFileName = argval 
    if (option in ("-h", "--help")): 
     usage() 
    return titlePresent, linesPerFile, sepChar, outputFileName 

def main(): 
    """ This is how we are called """ 
    try: 
    opts,args = getopt.getopt(sys.argv[1:], "tl:s:o:h", ["title", "lines=", "sep=", "output=", "help"]) 
    except getopt.GetoptError: 
    usage() 
    if (len(args) != 1): 
    usage() 
    inputFileName = args[0] 
    try: 
    inputFile = open(inputFileName, 'r') 
    except IOError: 
    print "File not found:", inputFileName, "...aborting" 
    sys.exit(-1) 
    titlePresent, linesPerFile, sepChar, outputFileName = validateOpts(opts) 
    if (outputFileName == ""): 
    outputFileName = getDefaultOutputFileName(inputFileName) 
    workbook, worksheet = openExcelSheet(outputFileName) 
    fno = 0 
    lno = 0 
    titleCols = [] 
    reader = csv.reader(inputFile, delimiter=sepChar) 
    for line in reader: 
    if (lno == 0 and titlePresent): 
     if (len(titleCols) == 0): 
     titleCols = line 
     writeExcelHeader(worksheet, titleCols) 
    else: 
     writeExcelRow(worksheet, lno, line) 
    lno = lno + 1 
    if (linesPerFile != -1 and lno >= linesPerFile): 
     closeExcelSheet(workbook, outputFileName) 
     renameOutputFile(outputFileName, fno) 
     fno = fno + 1 
     lno = 0 
     workbook, worksheet = openExcelSheet(outputFileName) 
    inputFile.close() 
    closeExcelSheet(workbook, outputFileName) 
    if (fno > 0): 
    renameOutputFile(outputFileName, fno) 

if __name__ == "__main__": 
    main() 
+1

雖然我對將CSV文件轉換爲XLS的好處持懷疑態度,但您可以使用子進程在循環中調用此腳本。 – ti7

+0

我沒有電腦上的python應該運行這個,所以我不能, – euranoo

回答

1

有許多方法來轉換csvxls以及在python xlsx(和我也同意@ ti7的懷疑的話),但我想包括令人印象深刻的途徑之一(的確非常方便)通過使用xlsxwriter模塊來完成。你也可以尋找openpyxl

import os 
import glob 
import csv 
from xlsxwriter.workbook import Workbook 


for csvfile in glob.glob(os.path.join('.', '*.csv')): 
workbook = Workbook(csvfile[:-4] + '.xlsx') 
worksheet = workbook.add_worksheet() 
with open(csvfile, 'rt', encoding='utf8') as f: 
    reader = csv.reader(f) 
    for r, row in enumerate(reader): 
     for c, col in enumerate(row): 
      worksheet.write(r, c, col) 
workbook.close() 

希望工程..

1

而是重新發明輪子的,我推薦使用熊貓。這是一個很棒的Python庫,用於處理數據。首先查看pandas.read_csv(返回pandas.DataFrame),然後pandas.DataFrame.to_excel

當你在它的時候,也從標準庫中查找​​。