python操作word(python-docx库)


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='Intense Quote')

document.add_paragraph(
'first item in unordered list', style='List Bullet'
)
document.add_paragraph(
'first item in ordered list', style='List Number'
)

document.add_picture('t.jpg', width=Inches(1.25))

records = (
(3, '101', 'Spam'),
(7, '422', 'Eggs'),
(4, '631', 'Spam, spam, eggs, and spam')
)

table = document.add_table(rows=1, cols=3,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 qty, id, desc in records:
row_cells = table.add_row().cells
row_cells[0].text = str(qty)
row_cells[1].text = id
row_cells[2].text = desc

document.add_page_break()

document.save('demo.docx')
 

效果:

 

 

excel转word

document = Document()

from xl import XlsRw
p = XlsRw(r"C:\Users\guochunbiao\PycharmProjects\untitled\商密-本地加密测试用例.xlsx",p=2)
lis = p.get_all_data()

for item in lis:
print(item)
casename=item[1]
condition =item[2]
step=item[3]
pro = item[4]
p = document.add_paragraph(casename)
table = document.add_table(rows=5, cols=2,style='Table Grid')

# hdr_cells = table.cell[0].rows
table.cell(0,1).width=Inches(8)
table.cell(0,0).text = '用例名称'
table.cell(1,0).text = '前置条件'
table.cell(2,0).text = '测试步骤'
table.cell(3,0).text = '预期结果'
table.cell(4,0).text = '实际结果'
table.cell(0, 1).text = casename
table.cell(1, 1).text = condition
table.cell(2, 1).text = step
table.cell(3, 1).text = pro
# table.cell(4, 1).text = '实际结果'

document.add_page_break()
document.save('demo7.docx')
 
————————————————
版权声明:本文为CSDN博主「guochunbiao0416」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/guochunbiao0416/article/details/109637308

posted @ 2021-04-07 22:29  天涯海角路  阅读(5)  评论(0)    收藏  举报