从 python32 进行 Python 打印
我无法让 Python 打印 Word 文档。我想做的是打开Word文档,打印它然后关闭它。我可以打开 Word 和 Word 文档:
import win32com.client
msword = win32com.client.Dispatch("Word.Application")
msword.Documents.Open("X:\Backoffice\Adam\checklist.docx")
msword.visible= True
接下来我尝试打印,
msword.activedocument.printout("X:\Backoffice\Adam\checklist.docx")
但出现“打印输出无效”错误。
有人可以解释一下如何从 Python 打印这个文件吗?我认为这可能就像更改“打印输出”一词一样简单。谢谢,我是Python新手。
I can't get Python to print a word doc. What I am trying to do is to open the Word document, print it and close it. I can open Word and the Word document:
import win32com.client
msword = win32com.client.Dispatch("Word.Application")
msword.Documents.Open("X:\Backoffice\Adam\checklist.docx")
msword.visible= True
I have tried next to print
msword.activedocument.printout("X:\Backoffice\Adam\checklist.docx")
I get the error of "print out not valid".
Could someone shed some light on this how I can print this file from Python. I think it might be as simple as changing the word "printout". Thanks, I'm new to Python.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
msword.ActiveDocument
为您提供当前活动文档。PrintOut
方法打印该文档:它不将文档文件名作为参数。来自 http://msdn.microsoft.com/en -us/library/aa220363(v=office.11).aspx:
具体来说,Word 正在尝试使用您的文件名作为可能设置的布尔
背景
True
在后台打印。编辑:
大小写很重要,而且错误有点奇怪。
msword.ActiveDocument.Printout()
应该打印它。msword.ActiveDocument.printout()
抛出错误,抱怨“PrintOut”不是属性。我认为内部发生的情况是,当你在属性上不匹配大小写时,Python 会尝试进行补偿,但它对于方法却不太正确。或者类似的事情。
ActiveDocument
和activedocument
可以互换,但PrintOut
和printout
则不能。msword.ActiveDocument
gives you the current active document. ThePrintOut
method prints that document: it doesn't take a document filename as a parameter.From http://msdn.microsoft.com/en-us/library/aa220363(v=office.11).aspx:
Specifically Word is trying to use your filename as a boolean
Background
which may be setTrue
to print in the background.Edit:
Case matters and the error is a bit bizarre.
msword.ActiveDocument.Printout()
should print it.msword.ActiveDocument.printout()
throws an error complaining that 'PrintOut' is not a property.I think what happens internally is that Python tries to compensate when you don't match the case on properties but it doesn't get it quite right for methods. Or something like that anyway.
ActiveDocument
andactivedocument
are interchangeable butPrintOut
andprintout
aren't.您可能必须使用
\\
转义反斜杠字符\
:编辑:解释
反斜杠通常用于声明特殊字符。例如
\n
是换行符的特殊字符。如果你想要一个文字\
你必须转义它。You probably have to escape the backslash character
\
with\\
:EDIT: Explanation
The backslash is usually used to declare special characters. For example
\n
is the special character for a new-line. If you want a literal\
you have to escape it.