2017-08-21 17 views
0

Excelシートを読み込んで値を含む行を返すレポートジェネレータを作成しようとしています。私が必要とするように動作するバージョンを作ったが、csvでしか動作しないのはこれが私の最初のコードマッシュアップであるが、それはうまくいった。私は今、条件付き書式設定(例えば、< 65赤色の場合、特定のセル値をハイライト表示する)を含めたいので、csvではなくxlsxシートで書き直す必要があります。以下 は、私は値を検索し、行を返すことができ ...これが動作するようになってで私の試みですが、2番目の実行でそれを介してそれだけで以前働いているので驚くべきことであるエラーxlsx書込みcell_valueエラー、新しいワークシートへの書き込み

AttributeError: 'Worksheet' object has no attribute 'cell_value'

を返します。

AttributeError: 'function' object has no attribute 'value'

ヘルプ、私は私が今やっている見当がつかない:コードをステップ実行すると、私はそれが.VALUEする変更しようとしたが、戻りは....私が欲しいの値をretunsそれがどんな意味をなすdoens't場合iは、CSVのための私の元のコードを投稿して幸せ「説明」

おかげ

import xlsxwriter 
import xlrd 
import os 
import xlwt 

# open original excelbook and access first sheet 
for excelDocs in os.listdir('.'): 
    if not excelDocs.endswith('.xlsx'): 
     continue # skip non-xlsx files 

    workbook = xlrd.open_workbook(excelDocs) 
    sheet = workbook.sheet_by_index(0) 
    cellslist = [] 
    i = 0 
#########WORKS!##################### 
    for row in range(sheet.nrows): 
     for col in range(sheet.ncols): 

      if sheet.cell_value(row, col) == 'CP' or sheet.cell_value(row, col) == 'LNA' or sheet.cell_value(row, col) == 'Last Name': 
       i = i + 1 
       data = [sheet.cell_value(0, col) for col in range(sheet.ncols)] 
       workbook = xlsxwriter.Workbook() 
       sheet = workbook.add_worksheet('excelDocs') 

       for index, value in enumerate(data): 
        sheet.write(i, index, value) 
       workbook = xlrd.open_workbook(excelDocs) 

答えて

1

私はxlsxwriter、xlrdまたはxlwt経験がありません。これがあなたの「第1回のコードマッシュ・アッ」であるので、私はopenpyxlを使って代替案を提供すると考えました。 私はあなたのデータを持っていないので、テストは少し難しいですが、構文エラーは修正される可能性があります。これが実行されない場合は私にお知らせください必要に応じて修正を支援します。

私はあなたの出力が別のファイル(ここではreport.xlsx)になっていると仮定し、チェックされた各ブックのタブ(各タブはソースブック名に名前が付けられています)を想定しています。

import openpyxl 
     from openpyxl import * 
     from openpyxl.utils import get_column_letter 

     interestingValues = ['CP','LNA', 'LastName'] 
     report = Workbook() 
     dest_filename = 'report.xlsx' 
     # open original excelbook and access first sheet 
     for excelDocs in os.listdir('.'): 
      if not excelDocs.endswith('.xlsx'): 
       continue # skip non-xlsx files 

      workbook = load_workbook(excelDocs) 
      sheet = workbook.active 
      workingReportSheet = report.create_sheet(str(excelDocs.split('.')[0])) 
      i = 0 
      for row in range(1,sheet.max_row): 
       for col in range(sheet.max_column): 
        columnLetter = get_column_letter(col +1) 
        if str(sheet['%s%s' % (columnLetter,row)].value) in interestingValues: 
         i += 1 
         data = [sheet['%s%s' % (str(get_column_letter(col)),i)].value for col in range(1,sheet.max_column +1)] 
         for index, value in enumerate(data): 
          workingReportSheet['%s%s' % (str(get_column_letter(index+1)),i)].value = value 

     report.save(filename = dest_filename) 
+0

あなたのpyexcelコードをお寄せいただきありがとうございます。ファイルごとに別々のシートを持つ出力ファイルが必要だったのは間違いありません。それは素晴らしいことです。 私はいくつか問題がありましたが、 columnletter = get_column_letter(col)が0を返しました...私は(col + 1)を追加して解決されたようです。 それは今返す: workingReportSheet( '%sの%s' は%(STR(get_column_letter(インデックス+ 1))、i))を値=値 はTypeError: 'ワークシート' オブジェクトは、それがpyexcelのように見える 呼び出すことはできませんより強力で、読み書きが可能です。のために書くのは簡単ですか?再度、感謝します。 – BrittleStiff

+0

これで、workingReportSheetに入力してください。 ()の代わりに使用されます。 –

+0

文字列が見つかったようですが、文字列を見つけた行の代わりに同じ(最初の)行を何度も繰り返し印刷しています。これは、元のコードでエラーが発生している可能性があります。 。 – BrittleStiff

1

コードをもう一度読んで、出力を破棄している可能性があります。 以下を試してください。

import xlsxwriter 
import xlrd 
import os 
import xlwt 

#Create output sheet 
outputworkbook = xlsxwriter.Workbook() 
# open original excelbook and access first sheet 
for excelDocs in os.listdir('.'): 
if not excelDocs.endswith('.xlsx'): 
    continue # skip non-xlsx files 

workbook = xlrd.open_workbook(excelDocs) 
sheet = workbook.sheet_by_index(0) 
cellslist = [] 
i = 0 
outputsheet = outputworkbook.add_worksheet('excelDocs') 
for row in range(sheet.nrows): 
    for col in range(sheet.ncols): 

     if sheet.cell_value(row, col) == 'CP' or sheet.cell_value(row, col) == 'LNA' or sheet.cell_value(row, col) == 'Last Name': 
      i = i + 1 
      data = [sheet.cell_value(0, col) for col in range(sheet.ncols)] 

      for index, value in enumerate(data): 
       outputsheet.write(i, index, value) 
関連する問題