Hi, trying to save the page as a JPG in python. It saves fine as a JPG, but will not compile, much less save with ExportBitmap.
My code is as follows:
import win32com.client as win32from win32com.client import Dispatch
cdrCenter = 9
def initCdr(): """ start up draw, fire up VBA and globals """ global cdr ## run Draw and return a handle cdr = Dispatch('CorelDraw.Application')
cdr.Visible = True ## Draw must have VBA turned on for automation to work cdr.InitializeVBA() return cdr ############################# EXAMPLES ######################### def hatch(): """ From """>vm.msun.ru/.../Rabota.htm""" hatch = cdr.CreateCurve(cdr.ActiveDocument) hatch.CreateSubPath(8.244488, 6.660827).AppendLineSegment(15.537358, -0.632043) sh = [] sh.append(cdr.ActiveLayer.CreateCurve(hatch)) shLine = sh[0] xOffset = 0.25 yOffset = 0.25 for i in range(1, 25): sh.append(shLine.Duplicate(xOffset, yOffset)) xOffset += 0.25 yOffset += 0.25 cdr.ActiveDocument.ReferencePoint = cdrCenter cdr.ActiveDocument.ClearSelection() for s in sh: s.AddToSelection() cdr.ActiveSelection.Stretch(-1, 1) cdr.ActiveSelection.Move(0, 0) sGrp = cdr.ActiveSelection.Group() sGrp.Stretch(0.5) #sGrp.Move(-4.498425, -0.232677) sGrp.Move(-10.611146, -0.286209)
def puzzlePiece(): """ community.coreldraw.com/.../application.initializevba """ crv = cdr.Application.CreateCurve(cdr.ActiveDocument) sp = crv.CreateSubPath(1.351, 8.545) sp.AppendCurveSegment(1.351, 8.926, 0.127, 89.901, 0.127, -64.56) sp.AppendCurveSegment(1.156, 8.952, 0.066, 115.44, 0.066, -48.906) sp.AppendCurveSegment(1.156, 9.15, 0.065, 131.09, 0.065, -133.149) sp.AppendCurveSegment(1.351, 9.163, 0.065, 46.846, 0.065, -116.315) sp.AppendCurveSegment(1.351, 9.545, 0.127, 63.683, 0.127, -89.902) sp.AppendCurveSegment(0.976, 9.545, 0.125, 179.951, 0.125, 25.612) sp.AppendCurveSegment(0.96, 9.342, 0.063, -154.391, 0.063, 40.943) sp.AppendCurveSegment(0.767, 9.339, 0.067, -139.06, 0.067, -41.987) sp.AppendCurveSegment(0.752, 9.547, 0.063, 138.014, 0.065, -33.906) sp.AppendCurveSegment(0.351, 9.545, 0.134, 146.087, 0.134, 0.045) sp.AppendCurveSegment(0.351, 9.163, 0.127, -90, 0.127, 63.681) sp.AppendCurveSegment(0.156, 9.15, 0.065, -116.317, 0.065, 46.846) sp.AppendCurveSegment(0.156, 8.952, 0.065, -133.152, 0.065, 131.093) sp.AppendCurveSegment(0.351, 8.926, 0.066, -48.906, 0.066, 115.439) sp.AppendCurveSegment(0.351, 8.545, 0.127, -64.561, 0.127, 90) sp.AppendCurveSegment(0.752, 8.547, 0.134, 0.002, 0.134, 146.087) sp.AppendCurveSegment(0.767, 8.339, 0.065, -33.908, 0.063, 138.012) sp.AppendCurveSegment(0.96, 8.342, 0.067, -41.987, 0.067, -139.058) sp.AppendCurveSegment(0.976, 8.545, 0.063, 40.943, 0.063, -154.388) sp.AppendCurveSegment(1.351, 8.545, 0.125, 25.613, 0.125, 179.998) sp.Closed = True s = cdr.ActiveLayer.CreateCurve(crv) s.Name = "puzzle" s.Move(3.861449, -3.133898) s.Fill.UniformColor.RGBAssign(175, 175, 175) # Gray # s.OrderToFront() # CreateSelection(ShapeArray() # cdr.ActiveDocument.CreateSelection(cdr.ActiveShape.Shapes("puzzle").Shapes("weld"), origSelection) # s1 = cdr.ActiveSelection.Combine() def welding(): left = 2 top = 7.5 right = 7 bottom = 4.25 ## CreateEllipse(left, top, right, bottom, [startAngle=90], [endAngle=90], [pie=False]) returns Shape ellipse = cdr.ActiveLayer.CreateEllipse(left, top, right, bottom) ## CreateRectangle(left, top, right, bottom, [cornerUL], [cornerUR], [cornerLR], [cornerLL]) returns Shape rect = cdr.ActiveLayer.CreateRectangle(left+4, top-1.25, right+.6, bottom+1.25) ## select the two shapes we just made cdr.ActiveDocument.ClearSelection() rect.AddToSelection() ellipse.AddToSelection() ## and weld them together weld = cdr.ActiveSelection.Weld(rect, True, True) ## make weld magenta weld.Fill.UniformColor.CMYKAssign(0, 100, 0, 0) weld.Name = "weld" ## get rid of the originals ellipse.Delete() rect.Delete() ## PDFVBASettings ## community.coreldraw.com/.../pdfvbasettingsdef publishToPDF(fullPath, cmyk=False, keywords='drawDataMerge'): """ fullPath = pdf filename drive:\\Path\\filename.ext cmyk = true grayscale = false keywords are meta data string for PDF search """ ## a few constants pdfVersion13 = 1 pdfWholeDocument = 0 pdfCurrentPage = 1 pdfSelection = 2 pdfPageRange = 3 pdfRGB = 0 pdfCMYK = 1 pdfGrayscale = 2 pdfNative = 3 doc = cdr.ActiveDocument doc.PDFSettings.Author = 'Danyell Frazoo' doc.PDFSettings.Bookmarks = True if cmyk: doc.PDFSettings.ColorMode = pdfCMYK else: doc.PDFSettings.ColorMode = pdfGrayscale doc.PDFSettings.ComplexFillsAsBitmaps = False doc.PDFSettings.EmbedBaseFonts = True doc.PDFSettings.EmbedFonts = True doc.PDFSettings.Hyperlinks = True doc.PDFSettings.Keywords = keywords doc.PDFSettings.Linearize = True doc.PDFSettings.pdfVersion = pdfVersion13 doc.PDFSettings.PublishRange = pdfWholeDocument doc.PDFSettings.TrueTypeToType1 = True doc.PublishToPDF(fullPath)
''' if I uncomment this, it throws an error '''# def savePageAsBitmap(jpgFileName): # cdrJPEG = 774 # cdrSelection = 2 # cdrRGBColorImage = 4 # filter = cdr.ActiveDocument.ExportBitmap(jpgFileName, cdrJPEG, cdrSelection, cdrRGBColorImage, UseColorProfile=False) # print filter # filter.Compression = 20 # filter.Optimized = True # res = True # if filter.HasDialog: # res = filter.ShowDialog() # if res: # filter.Finish # filter= None # cdr.ActiveDocument.Export("C:\Temp\Test.jpg", cdrJPEG, cdrCurrentPage) ########################################################## Main #######################################################################if __name__ == "__main__": global cdr documentPath = r'c:\temp' pdfFileName = 'example.pdf' jpgFileName = 'example.jpg'
cdr = initCdr() ## don't update screen while drawing, really speeds things up cdr.Optimization = True
## starts Draw initializes and creates a new document cdr.CreateDocument() ## EXAMPLES hatch() welding() puzzlePiece() print 'writing PDF... this can take a while... sometimes' publishToPDF(documentPath + '\\' + pdfFileName) print 'creating JPG' """ this part creates an error """ #savePageAsBitmap(documentPath + '\\' + jpgFileName) ## let Draw update screen and show our work cdr.Optimization = False cdr.ActiveWindow.Refresh() cdr.Application.Refresh()
For whatever reason, leading whitespace is lost.... how do you post code here?
Anyway with ExportBitmap un commented, I get the following error:
creating JPGTraceback (most recent call last): File "G:\Python Development\CorelDraw Office Com\CorelDraw\TenantLetterDevelopment\TenantLetterPy\shortExample.py", line 179, in <module> savePageAsBitmap(documentPath + '\\' + jpgFileName) File "G:\Python Development\CorelDraw Office Com\CorelDraw\TenantLetterDevelopment\TenantLetterPy\shortExample.py", line 143, in savePageAsBitmap filter = cdr.ActiveDocument.ExportBitmap(jpgFileName, cdrJPEG, cdrSelection, cdrRGBColorImage, UseColorProfile=False) File "C:\Python27\lib\site-packages\win32com\gen_py\FBF4300F-D921-11D1-B806-00A0C90646A9x0x14x0\IDrawDocument.py", line 187, in ExportBitmap , UseColorProfile, MaintainLayers, Compression, PaletteOptions, ExportAreaTypeError: The Python instance can not be converted to a COM object>>>
Anybody know how to use ExportBitmap from Com Automation???
Don't know any phyton.
But in VBA ActiveDocument.ExportBitmap returns a reference and not a value. So you have to write
Set expflt = ActiveDocument.ExportBitmap ...
Perhaps it helps a bit. If not ignore.
Thanks for the tip. Appreciated.