利用wxPython将word 文档转换为pdf文档

常常利用命令行来转换,觉得有时不是很顺手,尤其是对python一无所知的人更是如此.

索性编个GUI,选择一下文件,就可以自动转换了.经测试,转换的效果超过了Adobe,不知是为什么?(^_^).

如果你安装了word 2007和wxPython,那么可以试一试下面的程序:


# -*- coding: utf-8 -*- 
#---------------------------------------------------------------------------
# wxDoc2PDF.pyw::将word 2007/2003文件转换为pdf文件的wxPython GUI界面
# Author:Wu Xuping
# 2012-04-18
#---------------------------------------------------------------------------
import sys, os
import wx

from win32com.client import Dispatch, constants, gencache
#---------------------------------------------------------------------------

def doc2pdf(input, output):
    w = Dispatch("Word.Application") 
    try:
        doc = w.Documents.Open(input, ReadOnly = 1)
        doc.ExportAsFixedFormat(output, constants.wdExportFormatPDF, 
        Item = constants.wdExportDocumentWithMarkup, CreateBookmarks = constants.wdExportCreateHeadingBookmarks)
        return 0
    except:
        return 1
    finally:
        w.Quit(constants.wdDoNotSaveChanges)
        

# Generate all the support we can.
def GenerateSupport():
    gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}', 0, 8, 4)

#---------------------------------------------------------------------------
wildcard = "Word 2007/2003 File (*.docx *.doc)|*.doc*|"     \
           "All files (*.*)|*.*"
#---------------------------------------------------------------------------

class TestPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1)
        
        self.frame=parent

        b = wx.Button(self, -1, "Select Word 2007/2003 Files and Convert to PDF", (50,50),(480,80))
        self.Bind(wx.EVT_BUTTON, self.OnButton, b)


    def OnButton(self, evt):

        dlg = wx.FileDialog(
            self, message="Choose Word 2007/2003 file:",
            defaultDir=os.getcwd(), 
            defaultFile="",
            wildcard=wildcard,
            style=wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR
            )

        if dlg.ShowModal() == wx.ID_OK:
            paths = dlg.GetPaths()
            GenerateSupport()
            for path in paths:
                input = path
                output = os.path.splitext(input)[0]+'.pdf'
                self.frame.SetTitle(u'正在转换文档--->'+path)
                if (not os.path.isabs(input)):
                    input = os.path.abspath(input)
                if (not os.path.isabs(output)):
                    output = os.path.abspath(output)
                        
                doc2pdf(input, output)               
                
            self.frame.SetTitle('OK:-->'+output) 
            mdlg = wx.MessageDialog(None, u"转换已完成,是否继续转换?", u"标题信息", wx.YES_NO | wx.ICON_QUESTION)
            if mdlg.ShowModal() == wx.ID_YES:
                pass
            else:
                self.Close(True)
                wx.Exit()
            mdlg.Destroy()            
                

        dlg.Destroy()


#---------------------------------------------------------------------------

if __name__ == '__main__':
    app = wx.PySimpleApp(False)
    frame = wx.Frame(None,size = (600,250))
    frame.SetTitle('wxDoc2PDF::Wu Xuping')
    frame.Center(wx.BOTH)
    tp=TestPanel(frame)
    frame.Show()
    app.MainLoop()

给一幅效果图吧:


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值