2017-05-24 9 views
0

私は次のコードを使用してExcelファイルに書き込みます。私を修正してください。 私は、このコンテキストでHTMLページを解析しています。私の目的は、テーブル要素を見つけて列に書き込むことです。Python:AttributeError: 'str'オブジェクトに 'font'属性がありません

for row in table.findAll('tr', { "class" : "product-row" }): 
col = row.findAll('td') 

i=1 
Image = col[0].a.img['src'] 
Name = col[1].a.text 
Width = col[3].text 

record = (Image,Name,Width) 

book = xlwt.Workbook(encoding="utf-8") 
sheet1 = book.add_sheet("Sheet 1") 

sheet1.write(i, Image, Name, Width) 

book.save("trial.xls") 

それは次のエラーを示しています

--------------------------------------------------------------------------- 
AttributeError       Traceback (most recent call last) 
<ipython-input-179-1d146cb8794d> in <module>() 
    14  sheet1 = book.add_sheet("Sheet 1") 
    15 
---> 16  sheet1.write(i, Image, Name, Width) 
    17 
    18  book.save("trial.xls") 

C:\Users\Santosh\Anaconda3\lib\site-packages\xlwt\Worksheet.py in 
write(self, r, c, label, style) 
    1086   :class:`~xlwt.Style.XFStyle` object. 
    1087   """ 
-> 1088   self.row(r).write(c, label, style) 
    1089 
    1090  def write_rich_text(self, r, c, rich_text_list, 
style=Style.default_style): 

C:\Users\Santosh\Anaconda3\lib\site-packages\xlwt\Row.py in write(self, col, 
label, style) 
    233 
    234  def write(self, col, label, style=Style.default_style): 
--> 235   self.__adjust_height(style) 
    236   self.__adjust_bound_col_idx(col) 
    237   style_index = self.__parent_wb.add_style(style) 

C:\Users\Santosh\Anaconda3\lib\site-packages\xlwt\Row.py in 
__adjust_height(self, style) 
    63 
    64  def __adjust_height(self, style): 
---> 65   twips = style.font.height 
    66   points = float(twips)/20.0 
    67   # Cell height in pixels can be calcuted by following approx. 
formula: 

AttributeError: 'str' object has no attribute 'font 

答えて

0

あなたは正しく.write()メソッドを使用していません。行と列のインデックスに続けて、セルに書き込むデータを入力します。

record = (Image, Name, Width) 

for col_index, item in enumerate(record): 
    sheet1.write(i, col_index, item) 
+0

私はPythonの初心者です。行、列インデックスについてもっと教えてください。または私が参照できる任意の例。 – Santosh

+0

@Santosh確かに、メソッドの使い方と引数の意味を説明するメソッドのドキュメントをリンクしました。 – alecxe

+0

ありがとうございます。出来た!しかし、レコード全体を書く必要があります。だから私はそれらをループに入れる必要があると思う。 – Santosh

関連する問題