Python-docx: The example of what python-docx can do - doesn't run

Created on 11 Aug 2015  路  3Comments  路  Source: python-openxml/python-docx

Trying to run the sample code https://python-docx.readthedocs.org/en/latest/:

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='IntenseQuote')

document.add_paragraph(
            'first item in unordered list', style='ListBullet'
            )
document.add_paragraph(
            'first item in ordered list', style='ListNumber'
            )

document.add_picture('monty-truth.png', width=Inches(1.25))

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 item in recordset:
        row_cells = table.add_row().cells
        row_cells[0].text = str(item.qty)
        row_cells[1].text = str(item.id)
        row_cells[2].text = item.desc

document.add_page_break()

document.save('demo.docx')

Gives the following:

/usr/lib/python2.7/site-packages/docx/styles/styles.py:54: UserWarning: style lookup by style_id is deprecated. Use style name as key instead.
  warn(msg, UserWarning)
Traceback (most recent call last):
  File "docx1.py", line 30, in <module>
    for item in recordset:
NameError: name 'recordset' is not defined
documentation

Most helpful comment

I have made a few simple changes to make it run:

import urllib.request
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='IntenseQuote')

document.add_paragraph(
    'first item in unordered list', style='ListBullet'
)
document.add_paragraph(
    'first item in ordered list', style='ListNumber'
)

urllib.request.urlretrieve("http://placehold.it/350x150", "placeholder.png")
document.add_picture('placeholder.png', width=Inches(1.25))

recordset = [
    {
        "id" : 1,
        "qty": 2,
        "desc": "New item"
    },
    {
        "id" : 2,
        "qty": 2,
        "desc": "New item"
    },
    {
        "id" : 3,
        "qty": 2,
        "desc": "New item"
    },

]

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 item in recordset:
    row_cells = table.add_row().cells
    row_cells[0].text = str(item["qty"])
    row_cells[1].text = str(item["id"])
    row_cells[2].text = item["desc"]

document.add_page_break()

document.save('demo.docx')

All 3 comments

Yes, I made the mistake of assuming everyone would understand what that was and haven't gone back to correct it. The recordset in this case would be a sequence of objects that have a qty, id, and desc property, like you might receive back from SQLAlchemy or something. You'll have to interpolate for now how this applies to your situation. I'll leave this issue open as a reminder to fix it up in the docs.

I have made a few simple changes to make it run:

import urllib.request
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='IntenseQuote')

document.add_paragraph(
    'first item in unordered list', style='ListBullet'
)
document.add_paragraph(
    'first item in ordered list', style='ListNumber'
)

urllib.request.urlretrieve("http://placehold.it/350x150", "placeholder.png")
document.add_picture('placeholder.png', width=Inches(1.25))

recordset = [
    {
        "id" : 1,
        "qty": 2,
        "desc": "New item"
    },
    {
        "id" : 2,
        "qty": 2,
        "desc": "New item"
    },
    {
        "id" : 3,
        "qty": 2,
        "desc": "New item"
    },

]

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 item in recordset:
    row_cells = table.add_row().cells
    row_cells[0].text = str(item["qty"])
    row_cells[1].text = str(item["id"])
    row_cells[2].text = item["desc"]

document.add_page_break()

document.save('demo.docx')

Thanks for the help @berezovskyi. That was the great code which made me figure out the problem I am trying in different ways. I am a fresher in learning reading and writing into docx.This example shows clearly how to write into docx can you please explain how can I read text from doc. Please....!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ssuzen picture ssuzen  路  6Comments

achillis1 picture achillis1  路  5Comments

raphaelvalentin picture raphaelvalentin  路  7Comments

floatc picture floatc  路  4Comments

madphysicist picture madphysicist  路  3Comments