How to print docx and pdf using Java in Python?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LalangNina01
    New Member
    • Jun 2014
    • 1

    How to print docx and pdf using Java in Python?

    I actually can print using the txt file to printer, however, when I change to docx or pdf I get many undefined characters which cannot be understood at all. Do you guys have any ideas on how to solve this.

    The code I used.

    Code:
    import system
    import javax.print
    import java.io
    from java.lang import Thread
    
    filestream = java.io.BufferedInputStream(java.io.FileInputStream("C:/GEEKS.txt"))
    
    psInformat = javax.print.DocFlavor.INPUT_STREAM.AUTOSENSE
    
    myDoc = javax.print.SimpleDoc(filestream,psInformat,None)
    
    aset = javax.print.attribute.HashPrintRequestAttributeSet()
    aset.add(javax.print.attribute.standard.Copies(2))
    
    services = javax.print.PrintServiceLookup.lookupDefaultPrintService()
    
    job = services.createPrintJob()
    
    job.print(myDoc, None)
    
    Thread.sleep(20000)
    
    filestream.close()
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Why are you using Java inside Python? Is that a requirement? It looks like you're opening a file, getting a hash, looking up an attribute and printing it. I would imagine this is possible through Python without using the javax, but that is your decision on implementation.

    Anyway, as .docx is a Microsoft proprietary format, you would need to know how it's formatted, and import a library that is able to read it and get through all the cruft that MS put in there.

    From my Google search, it appears a docx is a zip'd openxml doc, so if you're doing it in Python you can import zipfile and use that. There also look to be a few custom-made libraries that will read docx's as well.

    Comment

    Working...