Python win32com - 按名称调用 TextBox?
我徒劳地尝试使用 pywin32 获取 MS Excel 中文本框的名称,我尝试录制宏并得到以下提示,但我无法将它们从 VBA 转换为 python。在下面的示例中,我选择文本框并重命名它,我想要的只是能够通过名称引用它们,我认为这让我很接近。
ActiveSheet.Shapes.Range(Array("TextBox 2")).Select
Selection.ShapeRange.Name = "TextBox 3"
到目前为止,这就是我在 python 中所拥有的。
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open("C:\\users\\khillstr\\Testing\\Scripts\\Book1.xlsx")
excel.Visible = True
ws = wb.Worksheets
for w in ws:
print w.Name
w.Activate
canvas = w.Shapes
for shp in canvas:
if shp.TextFrame.Characters:
print shp.TextFrame2.TextRange
I am trying in vain to get the name of a TextBox in MS Excel using pywin32, I have tried recording a macro and I get the following tips, but I cannot translate them from VBA to python. In the sample below I select the Textbox and rename it, all I want is to be able to refer to them by name and I figure this gets me close.
ActiveSheet.Shapes.Range(Array("TextBox 2")).Select
Selection.ShapeRange.Name = "TextBox 3"
This is what I have in python so far to this end.
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open("C:\\users\\khillstr\\Testing\\Scripts\\Book1.xlsx")
excel.Visible = True
ws = wb.Worksheets
for w in ws:
print w.Name
w.Activate
canvas = w.Shapes
for shp in canvas:
if shp.TextFrame.Characters:
print shp.TextFrame2.TextRange
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
名称是文本框形状的读/写属性。您可以使用该属性获取和设置名称。在 Excel 2010 中测试。
Name is a read/write property for textbox shapes. You can get and set the name using that property. Tested in Excel 2010.