2017-01-26 10 views
0

私は初心者です。私は既存のExcelファイルにデータを書き込もうとします。 robotframeworkを実行すると、「No keyword with name 'Excelファイルに書き込む」というメッセージが表示されます。だから、私のコードを修正したり、私に提案してもらうことができますか?既存のExcelファイル(XLS)に書き込む方法

import xlwt 
from xlutils.copy import copy 

class Excel(object): 

    def __init__(self): 
     print "write to excel file" 

    def group(self,lst, n): 

     return ([lst[i:i+n] for i in range(0,len(lst),n)]) 
    def write_to_excel_file(self,filename,content_list): 

      # Existing Excel File 
      w = copy(filename) 
      b = w.get_sheet(0) 

      # Create an new Excel file and add a worksheet. 
      #workbook = xlwt.Workbook() 
      #worksheet = workbook.add_sheet('wb') 

      #content_lists=[1,1,'hello',2,1,'brother',3,1,'how are you',4,1,'are you good today'] 
      t=self.group(content_list,3) 
      #print(t) 
      for item in t: 
       b.write(int(item[0]), int(item[1]), item[2]) 


      # close work book 
       w.save(filename) 
+0

はあなたがループの中で '' w.save(ファイル名)呼んでいるように注意してください。 –

+1

突然何が起こった、昨日あなたはそれを呼び出すことができました:) – Shijo

+0

@ Jean-FrançoisFabreあなたの提案をありがとう。 –

答えて

3

これはあなたのために働くなら、私が知ってみましょう

import xlwt 
from xlutils.copy import copy 
import xlrd 
import os.path 

class Excel(object): 

    def __init__(self): 
     print "write to excel file" 

    def group(self,lst, size): 
     return ([lst[i:i+size] for i in range(0, len(lst), size)]) 

    def write_to_excel_file(self,filename,content_list): 
     if not os.path.exists(filename): 
      workbook = xlwt.Workbook() # Create an new Excel file and add a worksheet. 
      worksheet = workbook.add_sheet('wb') #add worksheet 
      workbook.save(filename) 
     rb = xlrd.open_workbook(filename,formatting_info=True) 
     r_sheet = rb.sheet_by_index(0) 
     r = r_sheet.nrows 
     wb = copy(rb) 
     sheet = wb.get_sheet(0) 
     t=self.group(content_list,3) 
     for item in t: 
      sheet.write(int(item[0]), int(item[1]), item[2]) 
     wb.save(filename) 

RIDEテストケース

*** Settings *** 
Library   Collections 
Library   WriteExcel.Excel 

*** Variables *** 

*** Test Cases *** 
Write Excel Test first 
    [Tags] 
    @{content} Create List 
    Append To List ${content} 15 1 Test Case 1 
    Append To List ${content} 16 1 Test Case 2 
    Append To List ${content} 17 1 Test Case 3 
    Append To List ${content} 18 1 Test Case 4 
    Write To Excel File test3.xls ${content} 
    log @${content} 

Write Excel Test Second 
    [Tags] 
    @{content} Create List 
    Append To List ${content} 25 1 Test Case 11 
    Append To List ${content} 26 1 Test Case 12 
    Append To List ${content} 27 1 Test Case 13 
    Append To List ${content} 28 1 Test Case 14 
    Write To Excel File test3.xls ${content} 
    log @${content} 
+0

ありがとうございました。 これは完全に機能しています。 –

+0

@ a.arm答えをアップアップしていいです:) –

+0

@Shijo ExcelLibraryで使うと問題があります。それはIOError:[Errno 22]無効なモード( 'w + b')またはファイル名:u'test3.xls 'を表示します。この問題を解決するのに手伝ってもらえますか? *** ***セッティング 図書館コレクション 図書館WriteExcel.Excel 図書館\t ExcelLibrary ***変数*** ***テストケース*** 書き込みエクセルテスト最初 オープンエクセル現在のディレクトリ\t TEST3 .xls @ {content}リストを作成する $ {content} 15 1に追加するテストケース1 Excel3に書き込むtest3.xls $ {content} ログ@ $ {content} –

関連する問題