Skip to content

The example of what python-docx can do - doesn't run #198

Description

@ronbarak

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

Activity

  1. scanny commented on Aug 17, 2015

    @scanny
    Contributor

    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.

  2. berezovskyi commented on Nov 17, 2015

    @berezovskyi

    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')
  3. modified the milestone: Release v0.8.6 on Apr 9, 2016
  4. SaikrishnaThatikonda commented on Mar 26, 2018

    @SaikrishnaThatikonda

    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....!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions