2011-06-01 40 views
0

私は1つのコンパイルされたファイルに複数のファイルから従業員のタイムシート のコピー/貼り付けを自動化するスクリプトを記述しようとしています。彼らはプロジェクトコードのタイムシートであるため、従業員がその日に別のプロジェクトを担当していた場所には空白が残っています。また、ファイルはxlsrd(2007)からxlsrdがうまくいくように見える.csv.xlsに変換されています。エクセルのワークブックからデータを抽出し、python xlrd/xlwtを使用して別のデータに出力する方法は?

私はので、私は一般的なアルゴリズムが参考になるかもしれないと思ったブックオブジェクトを開き、作成する方法を知っていますが、このモジュールの私の知識は非常に限られている:

import xlrd, xlwt 

put all following in for or while loop to iterate through files: 
book = xlrd.open_workbook('mybook.csv.xls') 
extract data; store data for ouput 
use for loop to iterate over data, output to final sheet 
open next file, repeat process storing each output below the previous 

私は役立つものを探しています私は、コードだけでなく、答えを見つけます。 ご協力いただければ幸いです。ありがとう。

+0

を使用したいと思う別のワークブックと出力をエクセルs {A:T}と行{3:27}には、strとfloatのdtypesが含まれています。私はかなり既存の値の型の文字列を持つリストとnullまたは空の空白があれば作業が容易であることを確信しています。 – Corey

+1

Pythonでxlsxファイルを読み書きするには 'openpyxl'(https://bitbucket.org/ericgazoni/openpyxl/downloads)を使います。しかし、本当の解決策は、従業員がタイムシートをExcelファイルに保存することをやめることです。 –

+0

確かに。ありがとう、私はopenpyxlをチェックアウトします。また、私はあなたが本当に欲しいのでなければ、すでにこの問題のほとんどを処理しています。これ以上の情報は必要ありませんが、ありがとう! – Corey

答えて

1

これは...それは(日付が日付として残って、空のセルは、長さ0の内容、ブール値との誤差細胞とテキスト細胞は数細胞になることはないとなりません)できるだけ密接にあなたのデータを再生するに役立つかもしれません。

from xlrd import XL_CELL_EMPTY, XL_CELL_TEXT, XL_CELL_NUMBER, 
    XL_CELL_DATE, XL_CELL_BOOLEAN, XL_CELL_ERROR, open_workbook 
from xlwt import Row, easyxf, Workbook 

method_for_type = { 
    XL_CELL_TEXT: Row.set_cell_text, 
    XL_CELL_NUMBER: Row.set_cell_number, 
    XL_CELL_DATE: Row.set_cell_number, 
    XL_CELL_ERROR: Row.set_cell_error, 
    XL_CELL_BOOLEAN: Row.set_cell_boolean, 
    } 

date_style = easyxf(num_format_str='yyyy-mm-dd') 
other_style = easyxf(num_format_str='General') 

def append_sheet(rsheet, wsheet, wrowx=0): 
    for rrowx in xrange(rsheet.nrows): 
     rrowvalues = rsheet.row_values(rrowx) 
     wrow = wsheet.row(wrowx) 
     for rcolx, rtype in enumerate(rsheet.row_types(rrowx)): 
      if rtype == XL_CELL_EMPTY: continue 
      wcolx = rcolx 
      wmethod = method_for_type[rtype] 
      wstyle = date_style if rtype == XL_CELL_DATE else other_style 
      wmethod(wrow, wcolx, rrowvalues[rcolx], wstyle) 
     wrowx += 1 
    return wrowx 

if __name__ == '__main__': 
    import sys, xlrd, xlwt, glob 
    rdpattern, wtfname = sys.argv[1:3] 
    wtbook = Workbook() 
    wtsheet = wtbook.add_sheet('guff') 
    outrowx = 0 
    for rdfname in glob.glob(rdpattern): 
     rdbook = open_workbook(rdfname) 
     rdsheet = rdbook.sheet_by_index(0) 
     outrowx = append_sheet(rdsheet, wtsheet, outrowx) 
     print outrowx 
    wtbook.save(wtfname) 
+0

実際に私はあなたにメールするつもりだった! .csv.xlsが延長.CSVを使用して、「名前を付けて保存」でCSVにxl2007から変換されたファイルに与えられた拡張である、あなたとの接触に私の希望はxlsxrd、2007年のベータ版を試してみました、すべてのファイルから私は2007年を扱っています。私たちがほぼすべてのPythonユーザーであるため、部門全体にとっては素晴らしいことでしょう!また、私はopenpyxlを試してきましたが、xlrdは理解しやすく、より包括的な文書で使い始めました。可能であれば、それは大きく訴えかかります。 – Corey

+0

@Corey:あなたのメールアドレスを知ったらすぐにxlsxrdのベータ版を送付します。 –

+0

wcpapine | mtu.edu、ありがとう!私たちはそれをうまく使います。 – Corey

1

は私が最終的に図書館を作るかもしれないxlutils、xlrd、およびxlwtのためのExcel関数と呼ばれるクラスを作成しています。削除シート機能を作成しようとしています。

あなたは、彼らが容易であり、このための機能を持っているのでopenpyxlおよび/またはpyexcelに向かって移動する場合があります。ここで

はオープ​​ンpyxlを使用してコピーする方法です:あなたが1からデータを抽出したい場合はhttps://pyexcel.readthedocs.io/en/latest/

Copy whole worksheet with openpyxlここ

はxlwt、xlrd、およびxlutilsのラッパーですpyexcelのドキュメントです私がコラムを読んしようとしています。またcreateCopy(オリジナルのワークブック、他のワークブック、元のファイル名、新しいファイル名)

import xlwt 
import xlrd 
import xlutils.copy 
import xlutils class excelFunctions(): 

def getSheetNumber(self, fileName, sheetName): 

    # opens existing workbook 
    workbook = xlrd.open_workbook(fileName, on_demand=True) 

    #turns sheet name into sheet number  
    for index, sheet in enumerate(workbook.sheet_names()): 
     if sheet == sheetName: 
      return index 


def createSheet(self, fileName, sheetName): 

    # open existing workbook 
    rb = xlrd.open_workbook(fileName, formatting_info=True, on_demand=True) 

    # make a copy of it 
    wb = xl_copy(rb) 

    # creates a variable called sheets which stores all the sheet names 
    sheets = rb.sheet_names() 

    # creates a string which is equal to the sheetName user input 
    str1 = sheetName 

    # checks to see if the given sheetName is a current sheet 
    if (str1 not in sheets): 

     # add sheet to workbook with existing sheets 
     Sheet = wb.add_sheet(sheetName) 

     # save the sheet with the same file name as before 
     wb.save(fileName) 

    else: 
     # this declares the sheet variable to be equal to the sheet name the user gives 
     sheet = wb.get_sheet(self.getSheetNumber(fileName, sheetName)) 

     # save the sheet with the same file name as before 
     wb.save(fileName) 



def createCopy(self, fileName, fileName2, sheetName, sheetName2): 


    # open existing workbook 
    rb = xlrd.open_workbook(fileName, formatting_info=True) 

    # defines sheet as the name of the sheet given 
    sheet = rb.sheet_by_name(sheetName) 

    # makes a copy of the original sheet 
    wb = xl_copy(rb) 

    # creates an int called column_count which is equal to the sheets maximum columns 
    column_count = sheet.ncols - 1 

    # creates a blank array called stuff 
    Stuff = [] 

    # this loops through adding columns from the given sheet name 
    for i in range (0, column_count): 
     Stuff.append([sheet.cell_value(row, i) for row in range(sheet.nrows)]) 

    # create a sheet if there is not already a sheet  
    self.createSheet(fileName, sheetName2) 

    # defines sheet as the new sheet 
    sheet = wb.get_sheet(self.getSheetNumber(fileName, sheetName2)) 

    # this writes to the sheet 
    for colidx, col in enumerate(Stuff): 
     for rowidx, row in enumerate(col): 
      sheet.write(rowidx, colidx, row) 

    # this saves the file 
    wb.save(fileName2) 
+0

こんにちはニック、遅い応答のために申し訳ありません...私はちょうど今日ここにチェックインし、明らかにその間、信じられないほど長い間。どうやって来ているの?あなたの努力を続けましたか?私は現時点ではPythonを捨てて、あなたからの声を聞きたいと思っています...私の電子メールはまだ同じで、上記のコメントにあります...(少し遅れて天才のビットを元に戻すことはできません) – Corey

関連する問題