4

I need to fill in a document and then try and convert it into a PDF.

Any idea how I can do this?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
RadiantHex
  • 24,907
  • 47
  • 148
  • 244

3 Answers3

13

You can use OpenOffice if it is available on the system.

import subprocess
import shutil

input_filename = 'input.doc'
output_filename = 'output.pdf'

p = subprocess.Popen(['unoconv', '--stdout', input_filename], stdout=subprocess.PIPE)
with open(output_filename, 'w') as output:
   shutil.copyfileobj(p.stdout, output)

You can also look at unoconv's source code if you want to do it directly with the Python bindings for UNO/OpenOffice COM.

Rosh Oxymoron
  • 20,355
  • 6
  • 41
  • 43
2

Install a PDF printer driver like CutePDF.

Use COM automation to run MS Word; open the file, fill in the data, print the file as a PDF.

Alternatively: convert the Word file into a PDF form; use ReportLab to fill in the form.

Alternatively: print the Word file to a PDF file; use ReportLab to overlay text on the file.

Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
  • +1 Depending on the complexity of the Word Document, other 'compatible' tools are not going to generate the same output. Programmatically controlling Word may end up being the only solution for some people. – Samiuela Sep 08 '12 at 10:12
0

This would be an excellent place to start. It's free as in beer.

krs1
  • 1,125
  • 7
  • 16
  • 2
    And if there are images in the document, you'll probably need to use the Python Imaging Library: http://www.pythonware.com/products/pil or something similar. – Matt Jan 27 '11 at 16:30