By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,336 Members | 2,307 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,336 IT Pros & Developers. It's quick & easy.

How to print docx and pdf using Java in Python?

P: 1
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.

Expand|Select|Wrap|Line Numbers
  1. import system
  2. import javax.print
  3. import java.io
  4. from java.lang import Thread
  5.  
  6. filestream = java.io.BufferedInputStream(java.io.FileInputStream("C:/GEEKS.txt"))
  7.  
  8. psInformat = javax.print.DocFlavor.INPUT_STREAM.AUTOSENSE
  9.  
  10. myDoc = javax.print.SimpleDoc(filestream,psInformat,None)
  11.  
  12. aset = javax.print.attribute.HashPrintRequestAttributeSet()
  13. aset.add(javax.print.attribute.standard.Copies(2))
  14.  
  15. services = javax.print.PrintServiceLookup.lookupDefaultPrintService()
  16.  
  17. job = services.createPrintJob()
  18.  
  19. job.print(myDoc, None)
  20.  
  21. Thread.sleep(20000)
  22.  
  23. filestream.close()
Jun 26 '14 #1
Share this Question
Share on Google+
1 Reply


sicarie
Expert Mod 2.5K+
P: 4,677
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.
Jul 23 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.