Reputation: 4405
I am trying to set styles on an excel spreadsheet using pythons OPENPYXL module. I keep coming up with this error:
'str' object has no attribute 'BLACK'
Basically, my code reads a list of known values from a .xlsx file and places them into a python list. I use that list to compare the values in a column from an access table to make sure the values in each cell is correct as compared to the know values.
Where my script blows out is when I try to set styles using openpyxl. For some reason, the above error comes up. The wird thing is, I'm not even using BLACK in the styles anywhere and it seems to error out when I try to set the fill. In the SearchCursor portion of the script, it iterates through each row. It's on the second pass, that the script blows out. I have a feeling it wants to overwrite something, but I can't figure out what.
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
Upvotes: 1
Views: 3079
Reputation: 473863
The problem is in lines there you are defining colors. Just assign the color to style.fill.start_color.index
there. For example:
new_sheet.cell(None,x,fieldKey).style.fill.start_color.index = 'FF808000'
instead of:
new_sheet.cell(None,x,fieldKey).style.fill.start_color.index = openpyxl.style.Color = 'FF808000'
Upvotes: 2