2013-08-16 115 views
1

我想使用pythons OPENPYXL模塊在excel電子表格上設置樣式。我一直想出這個錯誤:openpyxl錯誤:'str'對象沒有屬性'BLACK'

'str' object has no attribute 'BLACK'

基本上,我的代碼讀取從.xlsx文件已知值的列表,並把它們放入一個Python列表。我使用該列表來比較訪問表中列中的值,以確保每個單元格中的值與已知值相比是正確的。

當我嘗試使用openpyxl設置樣式時,腳本被吹掉了。出於某種原因,上面的錯誤出現了。奇怪的是,我甚至沒有在任何地方使用BLACK,並且當我嘗試設置填充時似乎錯誤。在腳本的SearchCursor部分中,它遍歷每一行。第二遍,劇本爆發了。我有一種感覺,它想覆蓋一些東西,但我無法弄清楚什麼。

import openpyxl, arcpy 
from arcpy import env 
from openpyxl import Workbook 

env.workspace = r"Z:\Access_Tables\Access_Table.gdb" 

TableList = [] 
for row in arcpy.SearchCursor(r"Z:\Domains\Domains.xlsx\DOMAINS$"): 
    TableList.append(row.Code) 


# Create workbook for report. Openpyxl 
workbook = openpyxl.Workbook() 
ws = workbook.get_active_sheet() 
ws.title = "Test" 
workbook.remove_sheet(ws) 

# List out the access tables in the workspace 
for fc in arcpy.ListFeatureClasses(): 

    # Processing SOIL Point access table 
    if fc == "SOIL": 

     # List the column headings from the access table to be applied to the .xlsx table 
     fieldnames = [f.name for f in arcpy.ListFields(fc)]  
     # Create Sheet. Openpyxl 
     new_sheet = workbook.create_sheet(None,fc) 

     dictFieldnames = {} 
     for num,fname in enumerate(fieldnames): 
      dictFieldnames[num] = fname 

      # Write to cell, openpyxl     
      new_sheet.cell(None,0,num).value = fname 
      col_let = openpyxl.cell.get_column_letter(num + 1) 
      new_sheet.column_dimensions[col_let].width = len(fname) + 3 

     # Process SOIL Field 
     if "SOIL" in fieldnames: 

      # Set a counter and Loop through each row of the access table 
      x = 1  
      for row in arcpy.SearchCursor(fc): 

       for key, value in dictFieldnames.iteritems(): 
        if value == "SOIL": 
         fieldKey = key 

       if not row.SOIL or len(row.SOIL.strip()) == 0: 

        # Openpyxl write. Set fill and color for cell. Write the unique id to the cell. 
        new_sheet.cell(None,x,fieldKey).style.fill.fill_type = openpyxl.style.Fill.FILL_SOLID 
        new_sheet.cell(None,x,fieldKey).style.fill.start_color.index = openpyxl.style.Color = 'FF808000' 
        new_sheet.cell(None,x,fieldKey).value = row.OBJECTID 
        x += 1 
        print 'first' 
       elif len(row.INCLUSION_TYPE) not in range(2,5): 


        # Openpyxl write. Set fill and color for cell. Write the unique id to the cell. 
        new_sheet.cell(None,x,fieldKey).style.fill.fill_type = openpyxl.style.Fill.FILL_SOLID 
        new_sheet.cell(None,x,fieldKey).style.fill.start_color.index = openpyxl.style.Color = 'FF2F4F4F'      
        new_sheet.cell(None,x,fieldKey).value = row.OBJECTID       
        x += 1 
        print 'second' 
       elif row.SOIL.upper() not in [y.upper() for y in TableList]: 


        # Openpyxl write. Set fill and color for cell. Write the unique id to the cell. 
        new_sheet.cell(None,x,fieldKey).style.fill.fill_type = openpyxl.style.Fill.FILL_SOLID 
        new_sheet.cell(None,x,fieldKey).style.fill.start_color.index = openpyxl.style.Color = 'FF00FFFF'      
        new_sheet.cell(None,x,fieldKey).value = row.OBJECTID       
        x += 1 
        print 'third' 

       print x 

回答

2

問題是行中存在定義顏色的問題。只需將顏色分配給style.fill.start_color.index即可。例如:

new_sheet.cell(None,x,fieldKey).style.fill.start_color.index = 'FF808000' 

代替:

new_sheet.cell(None,x,fieldKey).style.fill.start_color.index = openpyxl.style.Color = 'FF808000' 
+0

輝煌。很明顯,我不相信我錯過了它。不過,第一次使用openpyxl。感謝@alecxe! – Mike

相關問題