Give me comments in this python code to understand import do
Give me comments in this python code to understand.
import docx
#read in an existing doc
doc1 = docx.Document(\'docxinput.docx\')
for para in range (0, len(doc1.paragraphs)):
print(doc1.paragraphs[para].text)
#create a new doc
doc = docx.Document()
doc.add_picture(\'ucdenvercse.png\',width=docx.shared.Inches(3))
doc.add_paragraph(\'Your first and Last Name\')
paraObj1 = doc.add_paragraph(\'This is a second paragraph.\')
paraObj1.add_run(\' This text is being added to the second paragraph.\')
doc.add_heading(\'Header 1\', 1)
doc.add_heading(\'Header 2\', 2)
doc.add_heading(\'Header 3\', 3)
# populate dataset
recordset = [
{
\"id\" : 1,
\"qty\": 2,
\"desc\": \"Thing 1\"
},
{
\"id\" : 3,
\"qty\": 4,
\"desc\": \"Thing 2\"
},
]
# add table ------------------
table = doc.add_table(1, 3) #1 row, 3 columns
# populate header row --------
table = doc.add_table(rows=1, cols=3, style = \'Table Grid\')
#table.style = \'Table Grid\'
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\"]
para = doc.add_paragraph()
para.add_run(\'This is on the\')
boldItalic=para.add_run(\' first\')
boldItalic.bold =True
boldItalic.italic = True
para.add_run(\' page!\')
currentPara =len(doc.paragraphs)
doc.paragraphs[currentPara-1].runs[2].add_break(docx.enum.text.WD_BREAK.PAGE)
para = doc.add_paragraph()
para.add_run(\'This is on the\')
boldItalic=para.add_run(\' second\')
boldItalic.bold =True
boldItalic.italic = True
para.add_run(\' page!\')
doc.save(\'docxoutput.docx\')
Solution
import docx
#read in an existing doc
doc1 = docx.Document(\'docxinput.docx\')
for para in range (0, len(doc1.paragraphs)):# to get all files in the folder
print(doc1.paragraphs[para].text)# to print the document
#create a new doc
doc = docx.Document()
doc.add_picture(\'ucdenvercse.png\',width=docx.shared.Inches(3))#add a picture to document
doc.add_paragraph(\'Your first and Last Name\')#add a paragraph to document
paraObj1 = doc.add_paragraph(\'This is a second paragraph.\')#creating a object to document
paraObj1.add_run(\' This text is being added to the second paragraph.\')
doc.add_heading(\'Header 1\', 1)#adding a header to the document
doc.add_heading(\'Header 2\', 2)
doc.add_heading(\'Header 3\', 3)
# populate dataset
recordset = [
{
\"id\" : 1,
\"qty\": 2,
\"desc\": \"Thing 1\"
},
{
\"id\" : 3,
\"qty\": 4,
\"desc\": \"Thing 2\"
},
]#create a recordset
# add table ------------------
table = doc.add_table(1, 3) #1 row, 3 columns
# populate header row --------
table = doc.add_table(rows=1, cols=3, style = \'Table Grid\')
#table.style = \'Table Grid\'
hdr_cells = table.rows[0].cells
hdr_cells[0].text = \'Qty\'#create a cell with heading qty
hdr_cells[1].text = \'Id\'#create a cell with heading id
hdr_cells[2].text = \'Desc\'#create a cell with heading desc
for item in recordset: #using for loops to get the data
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\"]
para = doc.add_paragraph() #creating and adding a new paragraph
para.add_run(\'This is on the\')#adding the line to para
boldItalic=para.add_run(\' first\')#adding as a bolditalic one
boldItalic.bold =True
boldItalic.italic = True
para.add_run(\' page!\')
currentPara =len(doc.paragraphs)#finding the length of the para
doc.paragraphs[currentPara-1].runs[2].add_break(docx.enum.text.WD_BREAK.PAGE)# adding a break to the paera
para = doc.add_paragraph()
para.add_run(\'This is on the\')#adding the line to para
boldItalic=para.add_run(\' second\')#adding as a bolditalic one
boldItalic.bold =True
boldItalic.italic = True
para.add_run(\' page!\')
doc.save(\'docxoutput.docx\')#save the file as docxoutput


