Repository navigation
The example of what python-docx can do - doesn't run #198
Description
Activity
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')
Reacted by simonwang2000, Kris Warner, kozzy911, Andrea Dal Pozzolo, Carson and Ethan CarlssonThanks 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....!
Trying to run the sample code
https://python-docx.readthedocs.org/en/latest/:Gives the following: