python win32转pdf 横版_.doc使用python转换为pdf

我尝试了接受的答案,但对Word产生的过大的PDF并不特别感兴趣,它通常比预期的大一个数量级。在查看了如何在使用虚拟PDF打印机时禁用对话框之后,我遇到了Bullzip PDF Printer,并且其功能给我留下了深刻的印象。现在,它取代了我以前使用的其他虚拟打印机。您可以在他们的下载页面上找到“免费社区版”。

该COM API,可以发现在这里,可以找到的可用设置列表在这里。设置被写入一个“ runonce”文件,该文件仅用于一个打印作业,然后自动删除。当打印多个PDF时,我们需要确保在开始另一个打印作业之前完成一个打印作业,以确保正确地使用每个文件的设置。

import os, re, time, datetime, win32com.client

def print_to_Bullzip(file):

util = win32com.client.Dispatch("Bullzip.PDFUtil")

settings = win32com.client.Dispatch("Bullzip.PDFSettings")

settings.PrinterName = util.DefaultPrinterName      # make sure we're controlling the right PDF printer

outputFile = re.sub("\.[^.]+$", ".pdf", file)

statusFile = re.sub("\.[^.]+$", ".status", file)

settings.SetValue("Output", outputFile)

settings.SetValue("ConfirmOverwrite", "no")

settings.SetValue("ShowSaveAS", "never")

settings.SetValue("ShowSettings", "never")

settings.SetValue("ShowPDF", "no")

settings.SetValue("ShowProgress", "no")

settings.SetValue("ShowProgressFinished", "no")     # disable balloon tip

settings.SetValue("StatusFile", statusFile)         # created after print job

settings.WriteSettings(True)                        # write settings to the runonce.ini

util.PrintFile(file, util.DefaultPrinterName)       # send to Bullzip virtual printer

# wait until print job completes before continuing

# otherwise settings for the next job may not be used

timestamp = datetime.datetime.now()

while( (datetime.datetime.now() - timestamp).seconds < 10):

if os.path.exists(statusFile) and os.path.isfile(statusFile):

error = util.ReadIniString(statusFile, "Status", "Errors", '')

if error != "0":

raise IOError("PDF was created with errors")

os.remove(statusFile)

return

time.sleep(0.1)

raise IOError("PDF creation timed out")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值