python实现生成Word、docx文件的方法分析
作者:longgb123 时间:2021-03-01 07:49:53
本文实例讲述了python实现生成Word、docx文件的方法。分享给大家供大家参考,具体如下:
http://python-docx.readthedocs.io/en/latest/index.html
生成word的利器!
一、快速开始
from docx import Document
document = 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 = True
run.italic = True
paragraph.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].cells
heading_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 Inches
document.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()
# or
with 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_PARAGRAPH
document = Document()
paragraph = document.add_paragraph()
paragraph_format = paragraph.paragraph_format
paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
左边整体缩进
from docx.shared import Inches
paragraph = document.add_paragraph()
paragraph_format = paragraph.paragraph_format
paragraph_format.left_indent = Inches(0.5)
右边整体缩进
from docx.shared import Pt
paragraph_format.right_indent = Pt(24)
首行缩进
paragraph_format.first_line_indent = Inches(-0.25)
从字体调节,字体大小
run = document.add_paragraph().add_run()
font = run.font
from docx.shared import Pt
font.size = Pt(10.5) # 5号字体
font.italic = True
font.underline = True
字体颜色
from docx.shared import RGBColor
font.color.rgb = RGBColor(0x42, 0x24, 0xE9)
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/longgb123/article/details/53390164
标签:python,Word,docx
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
在JScript中使用ADODB.Stream判断文件编码
2008-06-08 13:03:00
asp如何在聊天室实现趣味答题并计分功能?
2010-06-18 20:00:00
asp下为什么韩文字后面显示分号?
2011-03-10 11:07:00
SQL Server命令行导数据的2种方式
2010-07-26 14:48:00
如何用Python做一个微信机器人自动拉群
2023-04-07 10:50:09
10个最容易犯的HTML标签错误
2010-09-13 12:37:00
![](https://img.aspxhome.com/file/UploadPic/20109/13/01-23s.jpg)
解决hive中导入text文件遇到的坑
2023-06-30 16:28:26
优化MySQL数据库性能的八大“妙手”
2007-11-18 14:49:00
如何查询日期类型的数据?
2009-11-11 20:04:00
利用xslt对xml进行缩进格式化处理
2008-09-04 10:34:00
asp任何取得多个表单的值
2008-04-15 15:31:00
用browsercam做用户登录后页面的兼容性测试
2008-12-29 12:05:00
![](https://img.aspxhome.com/file/UploadPic/200812/29/20081226170940321-68s.png)
IE6局部调用PNG32合并图片
2009-03-11 21:24:00
![](https://img.aspxhome.com/file/UploadPic/20093/11/01-12s.jpg)
django中的ajax组件教程详解
2023-11-19 00:59:32
![](https://img.aspxhome.com/file/2023/6/99026_0s.jpg)
正则表达式学习笔记
2008-04-15 07:44:00
解读ASP.NET 5 & MVC6系列教程(9):日志框架
2023-06-30 06:10:57
常用SQL语句词典
2008-08-03 17:19:00
Python定义函数实现累计求和操作
2021-07-07 00:54:19
![](https://img.aspxhome.com/file/2023/7/66457_0s.jpg)
Python学习之字符串常用方法总结
2021-12-19 02:19:46
![](https://img.aspxhome.com/file/2023/1/67781_0s.png)
Yii框架实现乐观锁与悲观锁流程详解
2023-11-16 13:38:38