python实现生成Word、docx文件的方法分析
Python  /  管理员 发布于 7年前   204
本文实例讲述了python实现生成Word、docx文件的方法。分享给大家供大家参考,具体如下:
http://python-docx.readthedocs.io/en/latest/index.html
生成word的利器!
一、快速开始
from docx import Documentdocument = Document()
1、段落
加一个段落,下面paragraph 是前面内容的光标指向,后面再该处插入一句话。
paragraph = document.add_paragraph('Lorem ipsum dolor sit amet.')prior_paragraph = paragraph.insert_paragraph_before('Lorem ipsum')
后面加一句话
paragraph = document.add_paragraph('Lorem ipsum ')paragraph.add_run('dolor sit amet.')
添加段落风格
document.add_paragraph('Lorem ipsum dolor sit amet.', style='ListBullet')
使用blod、italic 等等
paragraph = document.add_paragraph('Lorem ipsum ')run = paragraph.add_run('dolor')run.bold = Truerun.italic = Trueparagraph.add_run('dolor').bold = True
2、标题
level表示标题的大小
document.add_heading('The role of dolphins', level=2)
3、分页
document.add_page_break()
4、表格
table = document.add_table(rows=2, cols=2)
访问方法:
取出来,单独赋值
cell = table.cell(0, 1)cell.text = 'parrot, possibly dead'
依然使用二维数组类似的索引。
row = table.rows[1]row.cells[0].text = 'Foo bar to you.'row.cells[1].text = 'And a hearty foo bar to you too sir!'
分清楚结构
for row in table.rows: for cell in row.cells: print(cell.text)
查看信息
row_count = len(table.rows)col_count = len(table.columns)
添加一行
row = table.add_row()
动态添加表格
table = document.add_table(1, 3)# 标题heading_cells = table.rows[0].cellsheading_cells[0].text = 'Qty'heading_cells[1].text = 'SKU'heading_cells[2].text = 'Description'# 添加内容for item in items: cells = table.add_row().cells cells[0].text = str(item.column1) cells[1].text = item.column2 cells[2].text = item.column3
5、添加图片
from docx.shared import Inchesdocument.add_picture('image-filename.png', width=Inches(1.25), height=Inches(1.25))
二、操作document
只能打开07之后的,会覆盖。
document = Document('existing-document-file.docx')document.save('new-file-name.docx')
打开文件
f = open('foobar.docx', 'rb')document = Document(f)f.close()# orwith open('foobar.docx', 'rb') as f: source_stream = StringIO(f.read())document = Document(source_stream)source_stream.close()...target_stream = StringIO()document.save(target_stream)
三、操作text
段落居中
from docx.enum.text import WD_ALIGN_PARAGRAPHdocument = Document()paragraph = document.add_paragraph()paragraph_format = paragraph.paragraph_formatparagraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
左边整体缩进
from docx.shared import Inchesparagraph = document.add_paragraph()paragraph_format = paragraph.paragraph_formatparagraph_format.left_indent = Inches(0.5)
右边整体缩进
from docx.shared import Ptparagraph_format.right_indent = Pt(24)
首行缩进
paragraph_format.first_line_indent = Inches(-0.25)
从字体调节,字体大小
run = document.add_paragraph().add_run()font = run.fontfrom docx.shared import Ptfont.size = Pt(10.5) # 5号字体font.italic = Truefont.underline = True
字体颜色
from docx.shared import RGBColorfont.color.rgb = RGBColor(0x42, 0x24, 0xE9)
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号