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 列表list使用介绍
Nov 30 Python
python爬虫正则表达式之处理换行符
Jun 08 Python
Python unittest单元测试框架总结
Sep 08 Python
在python中bool函数的取值方法
Nov 01 Python
与Django结合利用模型对上传图片预测的实例详解
Aug 07 Python
Python搭建代理IP池实现接口设置与整体调度
Oct 27 Python
python清空命令行方式
Jan 13 Python
基于python实现FTP文件上传与下载操作(ftp&sftp协议)
Apr 01 Python
Python xml、字典、json、类四种数据类型如何实现互相转换
May 27 Python
Django中日期时间型字段进行年月日时分秒分组统计
Nov 27 Python
Python如何配置环境变量详解
May 18 Python
如何在pycharm中快捷安装pip命令(如pygame)
May 31 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实现从ftp服务器上下载文件树到本地电脑的程序
2009/02/10 PHP
解析如何在PHP下载文件名中解决乱码的问题
2013/06/20 PHP
谈谈你对Zend SAPIs(Zend SAPI Internals)的理解
2015/11/10 PHP
jquery ajax 检测用户注册时用户名是否存在
2009/11/03 Javascript
JQuery入门——事件切换之toggle()方法应用介绍
2013/02/05 Javascript
dwz 如何去掉ajaxloading具体代码
2013/05/22 Javascript
Js nodeType 属性全面解析
2013/11/14 Javascript
扩展JS Date对象时间格式化功能的小例子
2013/12/02 Javascript
js中arguments,caller,callee,apply的用法小结
2014/01/28 Javascript
jquery实现可旋转可拖拽的文字效果代码
2016/01/27 Javascript
Jquery获取第一个子元素简单实例
2016/06/02 Javascript
jquery 实现回车登录详解及实例代码
2016/10/23 Javascript
jQuery实现判断控件是否显示的方法
2017/01/11 Javascript
vue+elementUI 复杂表单的验证、数据提交方案问题
2019/06/24 Javascript
vue-cli+iview项目打包上线之后图标不显示问题及解决方法
2019/10/16 Javascript
微信小程序上传图片并等比列压缩到指定大小的实例代码
2019/10/24 Javascript
一篇文章让你搞懂JavaScript 原型和原型链
2020/11/23 Javascript
python基于mysql实现的简单队列以及跨进程锁实例详解
2014/07/07 Python
利用django如何解析用户上传的excel文件
2017/07/24 Python
Python使用Phantomjs截屏网页的方法
2018/05/17 Python
python如何对链表操作
2020/10/10 Python
Keras保存模型并载入模型继续训练的实现
2021/02/20 Python
年终自我鉴定
2013/10/09 职场文书
英语演讲稿范文
2014/01/03 职场文书
物流管理专业求职信
2014/05/29 职场文书
小学生感恩老师演讲稿
2014/08/28 职场文书
社保代办委托书怎么写
2014/10/06 职场文书
2014年保洁工作总结
2014/11/24 职场文书
2015年优质护理服务工作总结
2015/04/08 职场文书
拉贝日记观后感
2015/06/05 职场文书
文化大革命观后感
2015/06/17 职场文书
归途列车观后感
2015/06/17 职场文书
2016教师校本研修心得体会
2016/01/08 职场文书
python 实现体质指数BMI计算
2021/05/26 Python
javascript之Object.assign()的痛点分析
2022/03/03 Javascript
TV动画《间谍过家家》公开PV
2022/03/20 日漫