python win32print txt_用python在windows系统中打印.doc

Print

Tim Golden > Python Stuff > Win32 How Do I...? > Print

Introduction

The requirement: to print

This is probably the most wide-ranging question I'll have to address here, and the one with the greatest disparity between the number and complexity of solutions and the simplicity of the requirement. The answer is: it all depends what you're trying to print, what tools you have at your disposal, and how much control you need.

If you simply have a "document" (read: file of a well-known type, associated with one application) you wish to print, and aren't too fussy about controlling, then you can use the ShellExecute approach. This works (assuming you have the corresponding applications installed) for Microsoft Office documents, PDF files, text files, and pretty much any major application. Try it and see.

The next most general case is where you have something, for example a text file or raw PCL, which you know you can send directly to a printer. In that case, you can use the win32print functions directly.

If you have an image to print, you can combine the power of the Python Imaging Library with the win32ui module to do a rough-and-ready but useful print to any printer.

If you have a fair amount of text to print, your best bet is to use the Reportlab PDF Toolkit and its Platypus document system to generate readable PDFs from any amount of text, and then use the ShellExecute technique to print it.

Standard document: use ShellExecute

Make use of the fact that within Win32, file types (in effect, extensions) can be associated with applications via command verbs. Typically the same application will handle all verbs (and typically those verbs are Open and Print) but that's not strictly necessary. This means that you can tell the OS to take your file and call whatever's necessary to print it.

Takes care of standard file types

No need to mess around with printer lists

Gives you no control

Only works for well-defined document-application pairings.

Only prints to defa

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python使用Win32com可以方便地与Windows系统进行交互,如操作Office软件、发送邮件等等。以下是我的一些心得体会: 1. 安装pywin32模块 在使用Win32com之前,需要先安装pywin32模块。可以通过pip install pywin32命令进行安装。 2. 使用win32com.client模块 使用Win32com时,需要导入win32com.client模块。该模块提供了许多常用的COM接口,如操作Office软件的接口、发送邮件的接口等。 3. 操作Office软件 使用Win32com可以方便地操作Office软件,如Word、Excel等。通过创建COM对象,可以打开Office软件并对其进行操作,如设置文本、表格、图表等。以下是一个简单的操作Word的示例代码: ```python import win32com.client as win32 # 创建Word对象 word = win32.gencache.EnsureDispatch('Word.Application') # 打开Word文档 doc = word.Documents.Open(r'C:\test.docx') # 遍历文档的段落 for para in doc.Paragraphs: print(para.Range.Text) # 关闭Word文档 doc.Close() # 退出Word应用程序 word.Quit() ``` 4. 发送邮件 使用Win32com可以方便地发送邮件。通过创建COM对象,可以连接到邮件服务器并发送邮件。以下是一个简单的发送邮件的示例代码: ```python import win32com.client as win32 # 创建Outlook对象 outlook = win32.Dispatch('Outlook.Application') # 创建邮件对象 mail = outlook.CreateItem(0) # 设置邮件收件人、主题和正文 mail.To = 'xxx@xxx.com' mail.Subject = '测试邮件' mail.Body = '这是一封测试邮件!' # 发送邮件 mail.Send() # 退出Outlook应用程序 outlook.Quit() ``` 总之,使用Win32com可以方便地与Windows系统进行交互,提高工作效率。但需要注意的是,使用Win32com时需要了解COM接口的相关知识,以便更好地进行开发。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值