0

I would like to convert a VBA script in win32com code in order to select and modify property of line shape in excel worksheet. I am able to select the line (I see the line selected in excel worksheet) but I'm not able to change the property.

Could someone help me?

VBA Code

Sheets("Maps").Select
Sheets("Maps").Shapes.Range(Array(item_in_review_2)).Select

With Selection.ShapeRange.Line
    .Weight = 3
    If .ForeColor.RGB = RGB(0, 0, 0) Then
        .ForeColor.RGB = RGB(0, 255, 0)
    Else
        .ForeColor.RGB = RGB(255, 0, 0)
    End If
End With

This is my code in python

import win32com.client as win32
import os

def rgbToInt(rgb):
    colorInt = rgb[0] + (rgb[1] * 256) + (rgb[2] * 256 * 256)
    return colorInt

xlApp = win32.gencache.EnsureDispatch('Excel.Application')
xlApp.Visible = True

wb = xlApp.Workbooks.Open('C:\\Users\\xxx\\PycharmProjects\\prova\\Book1.xlsx')
sht = wb.Worksheets('Maps')

OTS_review = 'test'

changeline = sht.Shapes(OTS_review).Select()

changeline.Width = 3

if changeline.ForeColor.RGB == rgbToInt((255,0,0)):
    changeline.ForeColor.RGB = rgbToInt((0, 255, 0))
else:
    changeline.ForeColor.RGB = rgbToInt((255, 0, 0))

This is error reported

Traceback (most recent call last):

File "C:/Users/xxxx/PycharmProjects/prova/xxx.py", line 18, in changeline.Width = 3

AttributeError: 'NoneType' object has no attribute 'Width'

Tim Stack
  • 3,209
  • 3
  • 18
  • 39
Blue Bird
  • 5
  • 2
  • Not sure but shouldn't `changeline = sht.Shapes(OTS_review).Select()` be without `.Select()`? – Pᴇʜ Oct 31 '19 at 10:13
  • If I remove .Select() the Error doesn't appear but following the code changeline.Width = 3 nothing happen. Morover I get the error – Blue Bird Oct 31 '19 at 11:02

1 Answers1

0

Try maybe change to this:

changeline = sht.Shapes(OTS_review)  # or maybe with .Select()
changeline.ShapeRange.Line.Width = 3
changeline.ShapeRange.Line.ForeColor.RGB = rgbToInt((0, 255, 0))

'or

changeline = sht.Shapes(OTS_review)
changeline.Line.Width = 3
changeline.Line.ForeColor.RGB = rgbToInt((0, 255, 0))

I resolve by changing Width and Weight

changeline = sht.Shapes(OTS_review)
changeline.Line.Weight = 3
changeline.Line.ForeColor.RGB = rgbToInt((0, 255, 0))
Blue Bird
  • 5
  • 2
barneyos
  • 586
  • 2
  • 5
  • 7
  • With .Select() return AttributeError: 'NoneType' object has no attribute 'ShapeRange' Without .Select() return AttributeError: '' object has no attribute 'ShapeRange' on line changeline.ShapeRange.Line.Width = 3 – Blue Bird Oct 31 '19 at 13:31
  • I've added new lines to my above answer – barneyos Oct 31 '19 at 14:16