You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
The example of what python-docx can do - doesn't run #198
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
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.
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....!
Activity
scanny commentedon Aug 17, 2015
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.
berezovskyi commentedon Nov 17, 2015
I have made a few simple changes to make it run:
SaikrishnaThatikonda commentedon Mar 26, 2018
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....!