python - openpyxl错误: 'str' object has no attribute 'BLACK'

标签 python openpyxl

我正在尝试使用 python OPENPYXL 模块在 Excel 电子表格上设置样式。我不断出现这个错误:

'str' object has no attribute 'BLACK'

基本上,我的代码从 .xlsx 文件中读取已知值的列表,并将它们放入 python 列表中。我使用该列表来比较访问表中的列中的值,以确保每个单元格中的值与已知值相比是正确的。

当我尝试使用 openpyxl 设置样式时,我的脚本崩溃了。由于某种原因,出现了上述错误。奇怪的是,我什至没有在任何地方的样式中使用黑色,并且当我尝试设置填充时似乎会出错。在脚本的 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

最佳答案

问题出在您定义颜色的行中。只需将颜色分配给 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'

关于python - openpyxl错误: 'str' object has no attribute 'BLACK' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18281767/

相关文章:

python - Python 中通过随机化减少文本文件

python - 使用 openpyxl 在表中输入空白行

python - 如何从Excel中提取指定行而不使用NaN

python - 在 python 中打开 xlsx 文件时出错

python - 使用 SQLalchemy ORM 和 SQLite 通过跨数据库联接构建查询

python - 层序的输入 0 与预期的 ndim=3 层不兼容,发现 ndim=2。收到完整形状 : [None, 1]

Python - 查找美元和分

python - 创建类似文件的对象作为数据缓冲区

python - 以高效的内存使用将 pandas 数据写入 Excel

python-3.x - 使用 Openpyxl 从标题或实例获取工作表索引