从 python32 进行 Python 打印

发布于 2024-12-13 23:59:28 字数 468 浏览 0 评论 0原文

我无法让 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 技术交流群。

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

发布评论

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

评论(2

夏至、离别 2024-12-20 23:59:28

msword.ActiveDocument 为您提供当前活动文档。 PrintOut 方法打印该文档:它不将文档文件名作为参数。

来自 http://msdn.microsoft.com/en -us/library/aa220363(v=office.11​​).aspx

expression.PrintOut(Background, Append, Range, OutputFileName, From, To, Item, 
  Copies, Pages, PageType, PrintToFile, Collate, FileName, ActivePrinterMacGX, 
  ManualDuplexPrint, PrintZoomColumn, PrintZoomRow, PrintZoomPaperWidth, 
  PrintZoomPaperHeight)

具体来说,Word 正在尝试使用您的文件名作为可能设置的布尔背景 True 在后台打印。

编辑
大小写很重要,而且错误有点奇怪。 msword.ActiveDocument.Printout() 应该打印它。 msword.ActiveDocument.printout() 抛出错误,抱怨“PrintOut”不是属性。

我认为内部发生的情况是,当你在属性上不匹配大小写时,Python 会尝试进行补偿,但它对于方法却不太正确。或者类似的事情。 ActiveDocumentactivedocument 可以互换,但 PrintOutprintout 则不能。

msword.ActiveDocument gives you the current active document. The PrintOut 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:

expression.PrintOut(Background, Append, Range, OutputFileName, From, To, Item, 
  Copies, Pages, PageType, PrintToFile, Collate, FileName, ActivePrinterMacGX, 
  ManualDuplexPrint, PrintZoomColumn, PrintZoomRow, PrintZoomPaperWidth, 
  PrintZoomPaperHeight)

Specifically Word is trying to use your filename as a boolean Background which may be set True 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 and activedocument are interchangeable but PrintOut and printout aren't.

说谎友 2024-12-20 23:59:28

您可能必须使用 \\ 转义反斜杠字符 \

msword.Documents.Open("X:\\Backoffice\\Adam\\checklist.docx")

编辑:解释

反斜杠通常用于声明特殊字符。例如 \n 是换行符的特殊字符。如果你想要一个文字 \ 你必须转义它。

You probably have to escape the backslash character \ with \\:

msword.Documents.Open("X:\\Backoffice\\Adam\\checklist.docx")

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.

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