Python docx Library Code Demonstration
- 2021-12-11 08:07:21
- OfStack
Catalog Python docx library code demo main business code test code (set font) summary
Python docx Library Code Demonstration
Installation
Need lxml
pip install python-docx
Master business code
from openpyxl import Workbook
from openpyxl import load_workbook
from docx import Document
from docx.oxml.ns import qn
from docx.shared import Pt,RGBColor,Cm
from docx.enum.style import WD_STYLE_TYPE
import os
# New Output
if os.path.exists('test_out.docx'):
os.remove('test_out.docx')
# Read a meter
i1 = load_workbook(' Business node analysis 20211022103647(1).xlsx')
print(' End of table reading ')
i = i1[' Business node analysis ']
out = []
# range(x,y) x To y-1 Row
for h in range(523,528):
lie = []
for l in i[h]:
lie.append(l.value)
out.append(lie[8])
print(out)
print(len(out))
print(' Wait to build word Document ')
# Create word
doc = Document()
doc.styles['Normal'].font.name = u' Song Style '
doc.styles['Normal'].font.size = Pt(10.5)
# Set bold style
style_hei = doc.styles.add_style('hei', WD_STYLE_TYPE.CHARACTER)
style_hei.font.name = ' Blackbody '
doc.styles['hei']._element.rPr.rFonts.set(qn('w:eastAsia'), u' Blackbody ')
# Set the style of Song characters
style_hei = doc.styles.add_style('song', WD_STYLE_TYPE.CHARACTER)
style_hei.font.name = ' Song Style '
doc.styles['song']._element.rPr.rFonts.set(qn('w:eastAsia'), u' Song Style ')
# Completion progress
# Segment 1
p1 = doc.add_paragraph()
# Text
r1 = p1.add_run(' Completion progress ',style='song')
r1.font.size = Pt(22)
r1.font.bold = True
p1.paragraph_format.space_before = Pt(17)
p1.paragraph_format.space_after = Pt(16.5)
p1.paragraph_format.line_spacing = 1.0
# Table 1
table1 = doc.add_table(rows=len(out)+1,cols=4,style='Table Grid')
table1.cell(0,0).paragraphs[0].add_run(' Serial number ',style='song')
# No. 1 2 OK cell Will change the original line width
table1.cell(0,0).width=Cm(2)
table1.cell(0,1).paragraphs[0].add_run(' Case name ',style='song')
table1.cell(0,2).paragraphs[0].add_run(' Test status ',style='song')
table1.cell(0,3).paragraphs[0].add_run(' Time ',style='song')
for i in range(1,len(out)+1):
table1.cell(i,0).paragraphs[0].add_run(str(i),style='song')
table1.cell(i,0).width=Cm(2)
table1.cell(i,1).paragraphs[0].add_run(out[i-1],style='song')
table1.cell(i,2).paragraphs[0].add_run(' To be tested / Under test / Finish ',style='song')
'''
# Product change
# Segment 2
p2 = doc.add_paragraph()
# Text
r2 = p2.add_run('\n1. Product change ',style='song')
r2.font.size = Pt(22)
r2.font.bold = True
p2.paragraph_format.space_before = Pt(17)
p2.paragraph_format.space_after = Pt(16.5)
p2.paragraph_format.line_spacing = 1.0
'''
# Precondition + Description of test steps
for i in range(1,len(out)+1):
st = str(i)+'.'+out[i-1]
# Segment 3
p3 = doc.add_paragraph()
# Text
r3 = p3.add_run(st,style='hei')
r3.font.size = Pt(16)
r3.font.bold = True
p3.paragraph_format.space_before = Pt(13)
p3.paragraph_format.space_after = Pt(13)
p3.paragraph_format.line_spacing = 1.0
# Segment 4
p4 = doc.add_paragraph()
# Text
r4 = p4.add_run(' Precondition ',style='song')
r4.font.size = Pt(18)
r4.font.bold = True
p4.paragraph_format.line_spacing = 1.0
# Table 2
table2 = doc.add_table(rows=4, cols=6,style ='Table Grid')
t0 = table2.cell(0,0).paragraphs[0].add_run(' Number ',style='song')
t0.font.size = Pt(12)
t0.font.color.rgb = RGBColor(0, 0, 255)
table2.cell(0,1).merge(table2.cell(0,2)).merge(table2.cell(0,3)).merge(table2.cell(0,4)).merge(table2.cell(0,5))
t1 = table2.cell(1,0).paragraphs[0].add_run('user_ID',style='song')
t1.font.size = Pt(12)
t1.font.color.rgb = RGBColor(0, 0, 255)
table2.cell(1,1).merge(table2.cell(1,2)).merge(table2.cell(1,3)).merge(table2.cell(1,4)).merge(table2.cell(1,5))
t2 = table2.cell(2,0).paragraphs[0].add_run('acct_ID',style='song')
t2.font.size = Pt(12)
t2.font.color.rgb = RGBColor(0, 0, 255)
table2.cell(2,1).merge(table2.cell(2,2)).merge(table2.cell(2,3)).merge(table2.cell(2,4)).merge(table2.cell(2,5))
t3 = table2.cell(3,0).paragraphs[0].add_run('cust_ID',style='song')
t3.font.size = Pt(12)
t3.font.color.rgb = RGBColor(0, 0, 255)
table2.cell(3,1).merge(table2.cell(3,2)).merge(table2.cell(3,3)).merge(table2.cell(3,4)).merge(table2.cell(3,5))
# Segment 5
p5 = doc.add_paragraph()
# Text
r5 = p5.add_run('\n Description of test steps ',style='song')
r5.font.size = Pt(18)
r5.font.bold = True
p5.paragraph_format.line_spacing = 1.0
# Table 3
table3 = doc.add_table(rows=2, cols=6,style ='Table Grid')
t0 = table3.cell(0,0).paragraphs[0].add_run('Order_id',style='song')
t0.font.size = Pt(12)
t0.font.color.rgb = RGBColor(0, 0, 255)
table3.cell(0,1).merge(table3.cell(0,2)).merge(table3.cell(0,3)).merge(table3.cell(0,4)).merge(table3.cell(0,5))
t1 = table3.cell(1,0).paragraphs[0].add_run('Trade_id',style='song')
t1.font.size = Pt(12)
t1.font.color.rgb = RGBColor(0, 0, 255)
table3.cell(1,1).merge(table3.cell(1,2)).merge(table3.cell(1,3)).merge(table3.cell(1,4)).merge(table3.cell(1,5))
# Segment 6
p6 = doc.add_paragraph()
# Text
r6 = p6.add_run('\n(1) Screenshot of test results foreground \n\n(2) Background verification of test results \n Correlation TRADE Table: \n Correlation SQL Verification: \n ① \n ② ',style='song')
r6.font.size = Pt(12)
r6.font.color.rgb = RGBColor(0, 0, 255)
p6.paragraph_format.space_after = Pt(10)
p6.paragraph_format.line_spacing = 1.5
# Save
doc.save('test_out.docx')
print(' Generate test_out.docx Success ')
Test code (set font)
from openpyxl import Workbook
from openpyxl import load_workbook
from docx import Document
from docx.oxml.ns import qn
from docx.shared import Pt,RGBColor
from docx.enum.style import WD_STYLE_TYPE
import os
document = Document() # New docx Document
style_song = document.styles.add_style('Song', WD_STYLE_TYPE.CHARACTER) # Settings Song Text type
style_song.font.name = ' Song Style '
document.styles['Song']._element.rPr.rFonts.set(qn('w:eastAsia'), u' Song Style ') # Will all fonts in a paragraph
style_song = document.styles.add_style('Kai', WD_STYLE_TYPE.CHARACTER)
style_song.font.name = ' Regular script '
document.styles['Kai']._element.rPr.rFonts.set(qn('w:eastAsia'), u' Regular script ') # Will all fonts in a paragraph
style_song = document.styles.add_style('Lishu', WD_STYLE_TYPE.CHARACTER)
style_song.font.name = ' Official script '
document.styles['Lishu']._element.rPr.rFonts.set(qn('w:eastAsia'), u' Official script ') # Will all fonts in a paragraph
paragraph1 = document.add_paragraph() # Add a paragraph
run = paragraph1.add_run(u'aBCDefg This is Chinese ', style='Song') # Set Song Style
font = run.font # Set fonts
font.name = 'Cambira' # Set Western fonts
paragraph1.add_run(u'aBCDefg This is Chinese ', style='Kai').font.name = 'Cambira'
paragraph1.add_run(u'aBCDefg This is Chinese ', style='Lishu').font.name = 'Cambira'
document.save('1.docx')
Summarize
This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!