Skip to content

Commit

Permalink
Merge pull request #13 from fahadadeel/master
Browse files Browse the repository at this point in the history
Aspose_Words_Java_for_Python Examples
  • Loading branch information
aspose-words-gists committed Jul 13, 2015
2 parents 9a8e651 + 69d777a commit 60994e4
Show file tree
Hide file tree
Showing 69 changed files with 3,961 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Plugins/Aspose_Words_Java_for_Python/default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from src.quickstart.helloworld.python import HelloWorld
import jpype
import os.path

asposeapispath = os.path.join(os.path.abspath("./"), "lib")

print "You need to put your Aspose.Words for Java APIs .jars in this folder:\n"+asposeapispath

jpype.startJVM(jpype.getDefaultJVMPath(), "-Djava.ext.dirs=%s" % asposeapispath)

hw = HelloWorld()
hw.main()
66 changes: 66 additions & 0 deletions Plugins/Aspose_Words_Java_for_Python/loadingandsaving/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
__author__ = 'fahadadeel'
import jpype
import shutil

class CheckFormat:

def __init__(self,dataDir):

self.dataDir = dataDir
self.Document = jpype.JClass("com.aspose.words.Document")
self.File = jpype.JClass("java.io.File")
self.LoadFormat = jpype.JClass("com.aspose.words.LoadFormat")
self.infoObj = jpype.JClass('com.aspose.words.FileFormatUtil')

def main(self):

supportedDir = self.dataDir + '/OutSupported/'
fileObj = self.File(self.dataDir)
filesList = fileObj.listFiles()
for file in filesList:
if file.isDirectory:
continue

nameOnly = file.getName()
print nameOnly

fileName = file.getPath()
print fileName

info = self.infoObj.detectFileFormat(fileName)
if (info.getLoadFormat() == self.LoadFormat.DOC):
print "Microsoft Word 97-2003 document."
elif (info.getLoadFormat() == self.LoadFormat.DOT):
print "Microsoft Word 97-2003 template."
elif (info.getLoadFormat() == self.LoadFormat.DOCX):
print "Office Open XML WordprocessingML Macro-Free Document."
elif (info.getLoadFormat() == self.LoadFormat.DOCM):
print "Office Open XML WordprocessingML Macro-Enabled Document."
elif (info.getLoadFormat() == self.LoadFormat.DOTX):
print "Office Open XML WordprocessingML Macro-Free Template."
elif (info.getLoadFormat() == self.LoadFormat.DOTM):
print "Office Open XML WordprocessingML Macro-Enabled Template."
elif (info.getLoadFormat() == self.LoadFormat.FLAT_OPC):
print "Flat OPC document."
elif (info.getLoadFormat() == self.LoadFormat.RTF):
print "RTF format."
elif (info.getLoadFormat() == self.LoadFormat.WORD_ML):
print "Microsoft Word 2003 WordprocessingML format."
elif (info.getLoadFormat() == self.LoadFormat.HTML):
print "HTML format."
elif (info.getLoadFormat() == self.LoadFormat.MHTML):
print "MHTML (Web archive) format."
elif (info.getLoadFormat() == self.LoadFormat.ODT):
print "OpenDocument Text."
elif (info.getLoadFormat() == self.LoadFormat.OTT):
print "OpenDocument Text Template."
elif (info.getLoadFormat() == self.LoadFormat.DOC_PRE_WORD_97):
print "MS Word 6 or Word 95 format."
elif (info.getLoadFormat() == self.LoadFormat.UNKNOWN):
print "Unknown format."
else :
print "Unknown format."

destFileObj = self.File(supportedDir + nameOnly)
destFile = destFileObj.getPath()
shutil.copy(fileName, destFile)
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
__author__ = 'fahadadeel'
import jpype

class MailMergeFormFields:

def __init__(self,dataDir):

self.dataDir = dataDir
self.Document = jpype.JClass("com.aspose.words.Document")

def main(self):

# Load the template document.
doc = self.Document(self.dataDir + "Template.doc")

# Setup mail merge event handler to do the custom work.

c = HandleMergeField()

proxy = jpype.JProxy("com.aspose.words.IFieldMergingCallback", inst=c)

doc.getMailMerge().setFieldMergingCallback(proxy)

# This is the data for mail merge.
fieldNames = ["RecipientName", "SenderName", "FaxNumber", "PhoneNumber",
"Subject", "Body", "Urgent", "ForReview", "PleaseComment"]
fieldValues = ["Josh", "Jenny", "123456789", "", "Hello",
"Test message 1", True, False, True]

# Execute the mail merge.
doc.getMailMerge().execute(fieldNames, fieldValues)

# Save the finished document.
doc.save(self.dataDir + "Template Out.doc")

class HandleMergeField:

def __init__(self):

self.DocumentBuilder = jpype.JClass("com.aspose.words.DocumentBuilder")
self.TextFormFieldType = jpype.JClass("com.aspose.words.TextFormFieldType")
self.mBuilder = self.DocumentBuilder()

def fieldMerging(self,e):

if (self.mBuilder is None):
self.mBuilder = self.DocumentBuilder(e.getDocument())

# We decided that we want all boolean values to be output as check box form fields.
if (e.getFieldValue() in ('True', 'False')) :
# Move the "cursor" to the current merge field.
self.mBuilder.moveToMergeField(e.getFieldName())

# It is nice to give names to check boxes. Lets generate a name such as MyField21 or so.
checkBoxName = e.getFieldName() + e.getRecordIndex()

# Insert a check box.
self.mBuilder.insertCheckBox(checkBoxName, e.getFieldValue(), 0)

# Nothing else to do for this field.
return

# Another example, we want the Subject field to come out as text input form field.
if ("Subject" == e.getFieldName()):

self.mBuilder.moveToMergeField(e.getFieldName())
textInputName = e.getFieldName() + e.getRecordIndex()
self.mBuilder.insertTextInput(textInputName, self.TextFormFieldType.REGULAR, "", e.getFieldValue(), 0)

def imageFieldMerging(self):

return

Binary file not shown.
Loading

0 comments on commit 60994e4

Please sign in to comment.