如何在openpyxl中使用预设颜色来执行纯色的PatternFill

时间:2018-01-15 14:30:32

标签: python openpyxl

我希望避免使用十六进制代码来处理颜色,并利用openpyxl中的预设颜色名称来填充单元格。但是我在这段代码中收到以下错误:

from openpyxl import Workbook
from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font, NamedStyle
from openpyxl.styles.colors import Color
from openpyxl.drawing.fill import ColorChoice

wb= Workbook()
ws = wb.active

sashighlight = NamedStyle(name="sashighlight")
bd = Side(style='thick', color="000000")
sashighlight.border = Border(left=bd, top=bd, right=bd, bottom=bd)
sashighlight.alignment = Alignment(horizontal='center', vertical='center')
sashighlight.fill = PatternFill(fill_type='solid', start_color=ColorChoice(prstClr='yellow'))

Traceback (most recent call last):
  File "test.py", line 13, in <module>
    sashighlight.fill = PatternFill(fill_type='solid', start_color=ColorChoice(prstClr='yellow'))
  File "/usr/lib/python2.7/site-packages/openpyxl/styles/fills.py", line 92, in __init__
    self.fgColor = fgColor
  File "/usr/lib/python2.7/site-packages/openpyxl/styles/colors.py", line 126, in __set__
    super(ColorDescriptor, self).__set__(instance, value)
  File "/usr/lib/python2.7/site-packages/openpyxl/descriptors/base.py", line 44, in __set__
    raise TypeError('expected ' + str(self.expected_type))

如何使用预设颜色名称?

1 个答案:

答案 0 :(得分:0)

你在找这样的东西(这对我有用):

public class GenerateEnv1 : MonoBehaviour {
    public GameObject []EnvTile;

    float tileZ = 29.31f;
    // Use this for initialization
    void Start () {
    }

    // Update is called once per frame
    void Update () {
    }

    void OnCollisionEnter (Collision col) {
        if(col.gameObject.tag =="ground"){
            GameObject o = Instantiate (EnvTile[Random.Range(0,4)], new Vector3(0f,0f,tileZ), Quaternion.EulerAngles(0,0,0));
            tileZ += 2.96f;
        }
    }
    void OnCollisionExit (Collision col) {
        if(col.gameObject.tag =="ground"){
            Destroy (col.gameObject,3); // this's i need to replace that in the new object 
        }
    }
}