1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
from docx import Document import os, sys from docx.shared import Inches import datetime
document = Document()
paragraph = document.add_paragraph("我很高兴见到你!")
prior_paragraph = paragraph.insert_paragraph_before("新插入的段落")
document.add_heading("添加一个标题,默认为顶级标题") document.add_heading("插入一个二级标题",level = 2) document.add_heading("插入一个0级标题",level = 0)
document.add_page_break()
table = document.add_table(rows = 2, cols = 2)
cell = table.cell(0,1)
cell.text = "朴"
row = table.rows[1] row.cells[0].text = "勇" row.cells[1].text = "毅"
for row in table.rows: for cell in row.cells: print(cell.text)
row_count = len(table.rows) col_count = len(table.columns) print("行列数为:",row_count,col_count)
row = table.add_row()
表格内容集 = { (1,"渗透测试","老司机是怎样炼成的"), (2,"等级测评","符合基本要求吗?"), (3,"安全服务","要啥有啥,干啥啥成"), }
示例表格 = document.add_table(1,3)
表格标题行 = 示例表格.rows[0].cells 表格标题行[0].text = "序号" 表格标题行[1].text = "信安研究室业务领域" 表格标题行[2].text = "描述"
for 表格内容 in 表格内容集: 新行 = 示例表格.add_row().cells 新行[0].text = str(表格内容[0]) 新行[1].text = 表格内容[1] 新行[2].text = 表格内容[2]
for 行 in 示例表格.rows: for 单元格 in 行.cells: print(单元格.text)
imgpath = os.path.join(sys.path[0],"img\\") print(imgpath) document.add_picture(imgpath+"1.jpg")
document.add_picture(imgpath+"1.jpg",width=Inches(1.0))
document.add_paragraph("应用段落样式,List Bullet", style = 'List Bullet') paragraph = document.add_paragraph('应用段落样式,List Bullet') paragraph.style = 'List Bullet'
paragraph = document.add_paragraph("厚德载物") run = paragraph.add_run("自强不息") run.bold = True run = paragraph.add_run('道法自然', 'Emphasis').italic = True
结果输出文件夹 = "Results" 结果文件夹绝对路径 = os.path.join(sys.path[0],结果输出文件夹) TheTime = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") FileName = 结果文件夹绝对路径+"\\"+TheTime+".docx" print(FileName) if(os.path.exists(结果文件夹绝对路径)): document.save(FileName) else: os.mkdir(结果文件夹绝对路径) document.save(FileName)
|