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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| from docx import Document from docx.enum.text import WD_ALIGN_PARAGRAPH, WD_PARAGRAPH_ALIGNMENT from docx.enum.text import WD_TAB_ALIGNMENT, WD_TAB_LEADER from docx.enum.text import WD_LINE_SPACING from docx.shared import Inches from docx.shared import Pt from docx.shared import RGBColor from docx.shared import Length from docx.oxml.ns import qn
document = Document()
section = document.sections[0] header = section.header bt1 = header.paragraphs[0] bt1.text = "Left Text\tCenter Text\tRight Text" bt1.style = document.styles["Header"]
document.styles['Normal'].font.name = '宋体' document.styles['Normal']._element.rPr.rFonts.set( qn('w:eastAsia'), '宋体') p = document.add_paragraph() p.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY p.paragraph_format.line_spacing_rule = WD_LINE_SPACING.ONE_POINT_FIVE p.paragraph_format.space_after = Pt(0) run = p.add_run('content') run.font.color.rgb = RGBColor(255, 0, 0) run.font.size = Pt(22) run.font.bold = True
p1 = document.add_paragraph('新增段落P1') pin1 = p1.insert_paragraph_before('在p1前插入段落pin1')
t1 = document.add_paragraph('此处Tetle信息', 'Title')
h1 = document.add_heading('此处默认标题1') h2 = document.add_heading('此处添加标题2', level=2) h3 = document.add_heading('此处添加标题3', level=3)
p2 = document.add_paragraph('新增段落p2并设置style类型', style='List Bullet') p3 = document.add_paragraph('新增段落p3并指定style类型') p3.style = 'List Bullet'
paragraph = document.add_paragraph() r1 = paragraph.add_run('通过.bold=True来设置粗体') r1.bold = True r1.style = 'Emphasis' r2 = paragraph.add_run('也可以') r3 = paragraph.add_run( '\n通过.italic=True来设置斜体,\n通过.font.size来设置字体大小,\n通过.font.color.rgb=RGBColor来设置字体颜色') r3.italic = True r3.font.size = Pt(20) r3.font.color.rgb = RGBColor(200, 77, 150)
p4 = document.add_paragraph('准备开始设置居中、左右对齐、缩进等') p4.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
p5 = document.add_paragraph('content123456789876543') p5.paragraph_format.left_indent = Inches(0.5) p5.paragraph_format.first_line_indent = Inches(0.5)
p5.paragraph_format.space_before = Pt(30) p5.paragraph_format.space_after = Pt(12)
p5.line_spacing_rule = WD_LINE_SPACING.EXACTLY p5.paragraph_format.line_spacing = Pt(18) p5.line_spacing_rule = WD_LINE_SPACING.MULTIPLE p5.paragraph_format.line_spacing = 1.75
p5.paragraph_format.keep_with_next = True
document.add_page_break() p5 = document.add_paragraph('.add_page_break()硬分页,即使文本未满')
table = document.add_table(rows=2, cols=2) table.style = 'Light Shading Accent 1'
cell = table.cell(0, 1) cell.text = '通过cell.text()来添加内容'
row = table.rows[1] row.cells[0].text = '通过.add_table(,)来添加表格' row.cells[1].text = '通过for row in table.rows内嵌套 for cell in row.cells来循环输出表格内容'
table1 = document.add_table(1, 3) table1.style = 'Light Shading Accent 2'
heading_cells = table1.rows[0].cells heading_cells[0].text = 'Qtx' heading_cells[1].text = 'Sku' heading_cells[2].text = 'Des'
for row in table.rows: for cell in row.cells: print(cell.text)
items = ( (7, '1024', 'plush kitens'), (3, '2042', 'furbees'), (1, '1288', 'french poodle collars,deluxe') )
for item in items: cells = table1.add_row().cells cells[0].text = str(item[0]) cells[1].text = str(item[1]) cells[2].text = str(item[2])
table = document.add_table(rows=3, cols=2, style='Table Grid')
hc = table.rows[0].cells hc[0].text = '姓名' hc[1].text = '年龄'
bc1 = table.rows[1].cells bc1[0].text = '张三' bc1[1].text = '22' bc2 = table.rows[2].cells bc2[0].text = '李四' bc2[1].text = '33'
document.add_picture('a4.png', width=Inches(2))
document.add_picture('a4.png', width=Inches(1.0), height=Inches(1.0))
document.save('test.docx')
|