Comtypes 没有属性“_Application" [英] Comtypes has no attribute '_Application'

查看:78
本文介绍了Comtypes 没有属性“_Application"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用 python 编写的文件转换脚本中遇到了一个奇怪的错误.我正在使用 comtypes 库将不同类型的文件转换为 pdf,出于某种原因,每当我尝试创建客户端时,我都会得到 module 'comtypes.gen.Excel' has no attribute '_Application'excel 应用程序的对象.我似乎无法在网上找到针对此问题的任何内容.大约一两个月前,脚本运行良好,所以我很困惑为什么它不再起作用了 - 我唯一能想到的就是 excel 更新或其他东西(如果这很重要的话).如果相关,我有办公室 2016.如果有人遇到此错误或有任何想法,将不胜感激.这是脚本,供参考:

I'm experiencing a weird bug in a file conversion script I wrote in python. I'm using the comtypes library to convert different types of files into pdfs and for some reason, I get module 'comtypes.gen.Excel' has no attribute '_Application' whenever I try to create a client object for an excel application. I can't seem to find anything online specific to this issue. The script was working fine about a month or two ago, so I'm confused as to why it isn't working anymore - the only thing I could think of was excel updating or something (if that would even matter). I have the office 2016 if that's relevant. If anyone has experienced this bug or has any ideas, help would be greatly appreciated. Here's the script, for reference:

import comtypes.client

excel = comtypes.client.CreateObject("Excel.Application") # exception here
excel.Visible = False
in_file = "INPUT_FILE"
out_file = "OUT_FILE"
f = excel.Workbooks.Open(in_file)
f.ExportAsFixedFormat(0, out_file, 1, 0)
f.Close()
excel.Close()

推荐答案

而不是使用 comtypes.client使用 pywin32

Instead of using comtypes.client Use pywin32

import win32com.client
#Replace excel=comtype.client.blablabla to
excel=win32com.client.DispatchEx("Excel.Application")

保持原样.

参考.https://github.com/shardulind/pdfconv_python/blob/master/convertor.py

这篇关于Comtypes 没有属性“_Application"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆