如何使用 win32com 自信地设置调度对象的属性

发布于 2024-10-21 01:07:10 字数 917 浏览 6 评论 0原文

我一直在努力将一个属性分配给一个对象(或者它是一个实例)

xl=win32com.client.Dispatch('Excel.Application')
xl.Visible=0
srce=xl.Workbooks.Open('myexcelfile')
srce.CheckCompatibility='False'

如果我查询 srce 对象的 CheckCompatibility 属性,我会得到“False”的响应

>>> srce.CheckCompatibility
    False

所以我天真地认为我可以保存这个无论如何我想要

<块引用> <块引用>

srce.SaveAs(r'c:\newtttxt14.xls',FileFormat=1)

但是当我这样做时,会出现兼容性检查器对话框。我点击继续,文件保存,然后再次检查兼容性。

>>> srce.CheckCompatibility
    True

So I again try to set it and this time I am successful

srce.CheckCompatibility='False'

我再次查询:

>>> srce.CheckCompatibility
False

现在,当我尝试保存文件时,兼容性检查器对话框不会出现,文件保存的正是我想要的。

在尝试以其他格式保存文件之前,我需要确定能够设置 srce 的属性 - 这可以通过其他方式发生吗?

谢谢

I have been struggling to assign an attribute to an object (or is it an instance)

xl=win32com.client.Dispatch('Excel.Application')
xl.Visible=0
srce=xl.Workbooks.Open('myexcelfile')
srce.CheckCompatibility='False'

If I query the srce object about its CheckCompatibility attribute I get a response of 'False'

>>> srce.CheckCompatibility
    False

So I am naively thinking I can save this anyway I want

srce.SaveAs(r'c:\newtttxt14.xls',FileFormat=1)

But when I do that the Compatibility Checker dialog comes up. I hit continue, the file saves and I then check the compatibility again.

>>> srce.CheckCompatibility
    True

So I again try to set it and this time I am successful

srce.CheckCompatibility='False'

I query it once more:

>>> srce.CheckCompatibility
False

Now when I try to save the file the Compatibility Checker Dialog does not appear, the file saves exactly was I want it to.

I need some certainty about being able to set the attribute of srce before I attempt to save the file in another format - can this happen in some other way?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

記柔刀 2024-10-28 01:07:10

我不确定,但我怀疑发生的情况是,以 XLS 形式保存文件会重置 CheckCompatibility 的值,因为对于 Excel 97-2003 二进制工作簿,该属性的默认值为 True。

无论如何,Excel 在保存文件时可能显示对话框的原因有多种,而不仅仅是兼容性检查器。我怀疑您真正想要的是抑制对话框,以便在脚本保存文件时不需要用户交互。 添加: 来抑制对话框。

>>> xl.DisplayAlerts = False

您可以通过在调用 srce.SaveAs(...) 之前

I'm not sure, but I suspect what's happening is that saving the file in XLS form is resetting the value of CheckCompatibility since the default value of that property is True for Excel 97-2003 binary workbooks.

In any case, there are multiple reasons why Excel might display a dialog when saving a file, not just the compatibility checker. I suspect what you really want is to suppress dialog boxes so that no user interactivity is required when your script saves a file. You can suppress dialog boxes by adding:

>>> xl.DisplayAlerts = False

before your call to srce.SaveAs(...).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文