1

The goal is to copy images I have as png's into a document (preferably a word document). I have found python-docx, but that only works for python 2.x (successfully downloaded) but it didn't run. The only thing I could find about it in this stack overflow question. It waspython-2.7 tagged. I don't mind if the pictures have to be in the same directory as the word document.

Sorry if I don't mentioning anything I should have, I am still fairly new at asking questions.

ZF007
  • 3,708
  • 8
  • 29
  • 48
wondercoll
  • 339
  • 1
  • 4
  • 15
  • docx as in [python-docx](https://pypi.org/project/python-docx/)? The project page claims that it works on both Python 2 and 3. – Kevin Jan 16 '20 at 19:14
  • yes, sorry, i will edit it to make it more clear – wondercoll Jan 16 '20 at 19:15
  • How can you tell that python-docx only works for 2.x? Did it fail to install when you did `pip install python-docx`? Did it fail to run when you did `import docx`? Did it run, but behave in a way you didn't expect? Please provide more detail. – Kevin Jan 16 '20 at 19:18
  • ok, it will be in the question – wondercoll Jan 16 '20 at 19:22
  • have you got some anaconda python environments up an running? If not install anacona python with version 3.7 or higher and an alternative environment with python==2.7.14 or so... There you install python-docx again and voila... It takes you about 1.5 hour to get it done ;-) – ZF007 Jan 16 '20 at 19:39
  • `python-docx` has run in Python 3 since long before that was cool :) If it's not working for you something else is wrong. – scanny Jan 16 '20 at 19:46
  • ..can confirm python-docx runs under python 3.7.5 and produces a docx file that can be opened with wordpad. – ZF007 Jan 16 '20 at 19:49
  • if you run your script from an editor like pycharm, jupyter notebook or komodo edit you get traceback with row numbers where things go wrong... maybe that's an option for you to traceback the issues? – ZF007 Jan 16 '20 at 20:01
  • Thank you all, I will try with anaconda – wondercoll Jan 16 '20 at 20:19
  • anaconda worked – wondercoll Jan 17 '20 at 15:40

1 Answers1

2

Proper python-docx installation:

  • from command-line: pip install python-docx

You should get 0.8.10 as latest. Approx 5.5mb large.

Below is the python-docx example code used from here. Look at the location where the files are located. if you adjust that to your liking than you should get a docx file.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from docx import Document
from docx.shared import Inches

document = Document()

document.add_heading('Document Title', 0)

p = document.add_paragraph('A plain paragraph having some ')
p.add_run('bold').bold = True
p.add_run(' and some ')
p.add_run('italic.').italic = True

document.add_heading('Heading, level 1', level=1)
document.add_paragraph('Intense quote', style='Intense Quote')

document.add_paragraph(
    'first item in unordered list', style='List Bullet'
)
document.add_paragraph(
    'first item in ordered list', style='List Number'
)

document.add_picture('D:\test\monty-truth.png', width=Inches(1.25))

records = (
    (3, '101', 'Spam'),
    (7, '422', 'Eggs'),
    (4, '631', 'Spam, spam, eggs, and spam')
)

table = document.add_table(rows=1, cols=3)
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Qty'
hdr_cells[1].text = 'Id'
hdr_cells[2].text = 'Desc'
for qty, id, desc in records:
    row_cells = table.add_row().cells
    row_cells[0].text = str(qty)
    row_cells[1].text = id
    row_cells[2].text = desc

document.add_page_break()

document.save('D:\test\demo.docx')

The monthy-truth.png file is here:

image

ZF007
  • 3,708
  • 8
  • 29
  • 48
  • I am going to try it in anaconda, but yes, I saw the website, thank you still:) – wondercoll Jan 16 '20 at 20:21
  • ..good to read :-) Tiny hint for if you want to test run experimental packages.. For example: `conda create -n py36test python=3.6` and `conda activate py36test` keeps your base environment clean :-) – ZF007 Jan 17 '20 at 15:45