python实现生成Word、docx文件的方法分析


Posted in Python onAugust 30, 2019

本文实例讲述了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程序设计有所帮助。

Python 相关文章推荐
Python continue语句用法实例
Mar 11 Python
Python删除空文件和空文件夹的方法
Jul 14 Python
python多线程方式执行多个bat代码
Jun 07 Python
Python堆排序原理与实现方法详解
May 11 Python
TensorFlow打印tensor值的实现方法
Jul 27 Python
解决python 无法加载downsample模型的问题
Oct 25 Python
解决pyinstaller打包exe文件出现命令窗口一闪而过的问题
Oct 31 Python
python 实现倒排索引的方法
Dec 25 Python
python利用wx实现界面按钮和按钮监听和字体改变的方法
Jul 17 Python
Python 改变数组类型为uint8的实现
Apr 09 Python
python如何实现递归转非递归
Feb 25 Python
pytorch MSELoss计算平均的实现方法
May 12 Python
python解析yaml文件过程详解
Aug 30 #Python
详细整理python 字符串(str)与列表(list)以及数组(array)之间的转换方法
Aug 30 #Python
python数据持久存储 pickle模块的基本使用方法解析
Aug 30 #Python
python 命令行传入参数实现解析
Aug 30 #Python
Python 在OpenCV里实现仿射变换—坐标变换效果
Aug 30 #Python
python在OpenCV里实现投影变换效果
Aug 30 #Python
python 模拟贷款卡号生成规则过程解析
Aug 30 #Python
You might like
获取远程文件大小的php函数
2010/01/11 PHP
探讨PHP调用时间格式的参数详解
2013/06/06 PHP
一些php项目中比较通用的php自建函数的详解
2013/06/06 PHP
JS getStyle获取最终样式函数代码
2010/04/01 Javascript
javascript 判断中文字符长度的函数代码
2012/08/27 Javascript
DWR实现模拟Google搜索效果实现原理及代码
2013/01/30 Javascript
jQuery获取选中内容及设置元素属性的方法
2014/07/09 Javascript
node.js中的fs.fstat方法使用说明
2014/12/15 Javascript
JS+CSS实现类似QQ好友及黑名单效果的树型菜单
2015/09/22 Javascript
Node.js读写文件之批量替换图片的实现方法
2016/09/07 Javascript
Bootstrap下拉菜单更改为悬停(hover)触发的方法
2017/05/24 Javascript
vue两个组件间值的传递或修改方式
2018/07/04 Javascript
vue.js绑定事件监听器示例【基于v-on事件绑定】
2018/07/07 Javascript
js实现简单选项卡功能
2020/03/23 Javascript
Vue 实现显示/隐藏层的思路(加全局点击事件)
2019/12/31 Javascript
Element的el-tree控件后台数据结构的生成以及方法的抽取
2020/03/05 Javascript
在Python的Flask框架下收发电子邮件的教程
2015/04/21 Python
python http接口自动化脚本详解
2018/01/02 Python
Python 实现两个列表里元素对应相乘的方法
2018/11/14 Python
python的继承知识点总结
2018/12/10 Python
PyCharm 配置远程python解释器和在本地修改服务器代码
2019/07/23 Python
django如何实现视图重定向
2019/07/24 Python
Python3并发写文件与Python对比
2019/11/20 Python
Python 模拟动态产生字母验证码图片功能
2019/12/24 Python
详解python对象之间的交互
2020/09/29 Python
如何使用 Flask 做一个评论系统
2020/11/27 Python
世界顶级足球门票网站:Live Football Tickets
2017/10/14 全球购物
俄罗斯香水在线商店:AromaCode
2019/12/04 全球购物
开会迟到检讨书
2014/02/03 职场文书
承诺书的格式范文
2014/03/28 职场文书
实习单位推荐信
2015/03/27 职场文书
心灵点滴观后感
2015/06/02 职场文书
推普标语口号大全
2015/12/26 职场文书
2016新教师岗前培训心得体会
2016/01/08 职场文书
SQL基础的查询语句
2021/11/11 MySQL
MySQL transaction事务安全示例讲解
2022/06/21 MySQL