如何在 win32com 中将属性更改为 excel 线形转换 vba 脚本
How to change attribute to excel line shape converting vba script in win32com
我想将 win32com
代码中的 VBA
脚本转换为 select 并修改 excel 工作表中的线形 属性。
我可以 select 该行(我在 excel 工作表中看到 select 行)但我无法更改 属性.
有人可以帮助我吗?
VBA代码
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
这是我在 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))
这是错误报告
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'
尝试改成这样:
changeline = sht.Shapes(OTS_review) # or maybe with .Select()
changeline.ShapeRange.Line.Width = 3
changeline.ShapeRange.Line.ForeColor.RGB = rgbToInt((0, 255, 0))
'或
changeline = sht.Shapes(OTS_review)
changeline.Line.Width = 3
changeline.Line.ForeColor.RGB = rgbToInt((0, 255, 0))
我通过更改宽度和粗细来解决
changeline = sht.Shapes(OTS_review)
changeline.Line.Weight = 3
changeline.Line.ForeColor.RGB = rgbToInt((0, 255, 0))
我想将 win32com
代码中的 VBA
脚本转换为 select 并修改 excel 工作表中的线形 属性。
我可以 select 该行(我在 excel 工作表中看到 select 行)但我无法更改 属性.
有人可以帮助我吗?
VBA代码
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
这是我在 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))
这是错误报告
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'
尝试改成这样:
changeline = sht.Shapes(OTS_review) # or maybe with .Select()
changeline.ShapeRange.Line.Width = 3
changeline.ShapeRange.Line.ForeColor.RGB = rgbToInt((0, 255, 0))
'或
changeline = sht.Shapes(OTS_review)
changeline.Line.Width = 3
changeline.Line.ForeColor.RGB = rgbToInt((0, 255, 0))
我通过更改宽度和粗细来解决
changeline = sht.Shapes(OTS_review)
changeline.Line.Weight = 3
changeline.Line.ForeColor.RGB = rgbToInt((0, 255, 0))