Word to PDF converter in python
import wx
import wx.xrc
import sys
import os
import comtypes.client
###########################################################################
## Class MyFrame1
###########################################################################
wdFormatPDF = 17
class MyFrame1 ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 500,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
bSizer1 = wx.BoxSizer( wx.VERTICAL )
self.m_panel1 = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
self.m_panel1.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_ACTIVECAPTION ) )
bSizer2 = wx.BoxSizer( wx.VERTICAL )
bSizer4 = wx.BoxSizer( wx.VERTICAL )
self.m_panel2 = wx.Panel( self.m_panel1, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
self.m_panel2.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_ACTIVECAPTION ) )
bSizer4.Add( self.m_panel2, 1, wx.EXPAND |wx.ALL, 5 )
bSizer2.Add( bSizer4, 1, wx.EXPAND, 5 )
bSizer5 = wx.BoxSizer( wx.VERTICAL )
self.m_staticText1 = wx.StaticText( self.m_panel1, wx.ID_ANY, u"Document to PDF converter", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText1.Wrap( -1 )
self.m_staticText1.SetFont( wx.Font( 12, 74, 90, 92, False, "Arial" ) )
self.m_staticText1.SetForegroundColour( wx.Colour( 45, 132, 23 ) )
bSizer5.Add( self.m_staticText1, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
bSizer2.Add( bSizer5, 1, wx.EXPAND, 5 )
bSizer6 = wx.BoxSizer( wx.HORIZONTAL )
self.m_staticText2 = wx.StaticText( self.m_panel1, wx.ID_ANY, u"Source :", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText2.Wrap( -1 )
self.m_staticText2.SetFont( wx.Font( 10, 74, 90, 92, False, "Arial" ) )
bSizer6.Add( self.m_staticText2, 0, wx.ALL, 5 )
self.m_textCtrl1 = wx.TextCtrl( self.m_panel1, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size( 300,-1 ), 0 )
bSizer6.Add( self.m_textCtrl1, 0, wx.ALL, 5 )
self.m_button1 = wx.Button( self.m_panel1, wx.ID_ANY, u"Browse", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_button1.SetFont( wx.Font( 10, 74, 90, 92, False, "Arial" ) )
bSizer6.Add( self.m_button1, 0, wx.ALL, 5 )
bSizer2.Add( bSizer6, 1, wx.EXPAND, 5 )
bSizer7 = wx.BoxSizer( wx.HORIZONTAL )
self.m_staticText3 = wx.StaticText( self.m_panel1, wx.ID_ANY, u"Destination :", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText3.Wrap( -1 )
bSizer7.Add( self.m_staticText3, 0, wx.ALL, 5 )
self.m_textCtrl2 = wx.TextCtrl( self.m_panel1, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size( 285,-1 ), 0 )
bSizer7.Add( self.m_textCtrl2, 0, wx.ALL, 5 )
self.m_button3 = wx.Button( self.m_panel1, wx.ID_ANY, u"Browse", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_button3.SetFont( wx.Font( 10, 74, 90, 92, False, "Arial" ) )
bSizer7.Add( self.m_button3, 0, wx.ALL, 5 )
bSizer2.Add( bSizer7, 1, wx.EXPAND, 5 )
bSizer9 = wx.BoxSizer( wx.HORIZONTAL )
self.m_staticText4 = wx.StaticText( self.m_panel1, wx.ID_ANY, u"Document name : ", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText4.Wrap( -1 )
bSizer9.Add( self.m_staticText4, 0, wx.ALL, 5 )
self.m_textCtrl3 = wx.TextCtrl( self.m_panel1, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size( 350,-1 ), 0 )
bSizer9.Add( self.m_textCtrl3, 0, wx.ALL, 5 )
bSizer2.Add( bSizer9, 1, wx.EXPAND, 5 )
bSizer10 = wx.BoxSizer( wx.VERTICAL )
self.m_button2 = wx.Button( self.m_panel1, wx.ID_ANY, u"Convert", wx.DefaultPosition, wx.Size( 150,50 ), 0 )
self.m_button2.SetFont( wx.Font( 10, 74, 90, 92, False, "Arial" ) )
self.m_button2.SetForegroundColour( wx.Colour( 177, 74, 39 ) )
bSizer10.Add( self.m_button2, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
bSizer2.Add( bSizer10, 1, wx.EXPAND, 5 )
bSizer8 = wx.BoxSizer( wx.VERTICAL )
self.m_panel3 = wx.Panel( self.m_panel1, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
self.m_panel3.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_ACTIVECAPTION ) )
bSizer8.Add( self.m_panel3, 1, wx.EXPAND |wx.ALL, 5 )
bSizer2.Add( bSizer8, 1, wx.EXPAND, 5 )
self.m_panel1.SetSizer( bSizer2 )
self.m_panel1.Layout()
bSizer2.Fit( self.m_panel1 )
bSizer1.Add( self.m_panel1, 1, wx.EXPAND, 5 )
self.SetSizer( bSizer1 )
self.Layout()
self.Centre( wx.BOTH )
# Connect Events
self.m_button1.Bind( wx.EVT_BUTTON, self.OnOpenDialogClick )
self.m_button3.Bind( wx.EVT_BUTTON, self.OnDirDialogClick )
self.m_button2.Bind( wx.EVT_BUTTON, self.ConvertToPDF )
def __del__( self ):
pass
# Virtual event handlers, overide them in your derived class
def OnOpenDialogClick( self, event ):
with wx.FileDialog(self, "Open Word Document File", wildcard="MS Word files (*.docx)|*.docx",
style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog:
if fileDialog.ShowModal() == wx.ID_OK:
path = fileDialog.GetPath()
self.m_textCtrl1.SetValue(path)
def OnDirDialogClick( self, event ):
dlg = wx.DirDialog(self, "Choose a Folder:",
style=wx.DD_DEFAULT_STYLE )
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
self.m_textCtrl2.SetValue(path)
dlg.Destroy()
def ConvertToPDF( self, event ):
in_file = self.m_textCtrl1.GetValue()
out_file = self.m_textCtrl2.GetValue() +"\\" + self.m_textCtrl3.GetValue()
word = comtypes.client.CreateObject('Word.Application')
doc = word.Documents.Open(in_file)
doc.SaveAs(out_file, FileFormat=wdFormatPDF)
doc.Close()
word.Quit()
App = wx.App(False)
frame = MyFrame1(None)
frame.Show()
App.MainLoop()
*******************
End of code*********************
Comments
Post a Comment