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有证书的加密解密实现方法
Nov 19 Python
python实现自动更换ip的方法
May 05 Python
Python对文件操作知识汇总
May 15 Python
Python smtplib实现发送邮件功能
May 22 Python
Python简单读写Xls格式文档的方法示例
Aug 17 Python
Python面向对象程序设计OOP深入分析【构造函数,组合类,工具类等】
Jan 05 Python
Python设计模式之备忘录模式原理与用法详解
Jan 15 Python
Python2与Python3的区别实例总结
Apr 17 Python
Pandas-Cookbook 时间戳处理方式
Dec 07 Python
Django-rest-framework中过滤器的定制实例
Apr 01 Python
快速解决pymongo操作mongodb的时区问题
Dec 05 Python
Python-split()函数实例用法讲解
Dec 18 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
现磨咖啡骗局!现磨咖啡=新鲜咖啡?现磨咖啡背后的猫腻你不懂!
2019/03/28 冲泡冲煮
PHP脚本数据库功能详解(上)
2006/10/09 PHP
php插入中文到sqlserver 2008里出现乱码的解决办法分享
2012/07/19 PHP
php eval函数用法 PHP中eval()函数小技巧
2012/10/31 PHP
php验证邮箱和ip地址最简单方法汇总
2015/10/30 PHP
php读取txt文件并将数据插入到数据库
2016/02/23 PHP
语义化 H1 标签
2008/01/14 Javascript
js实现键盘上下左右键选择文字并显示在文本框的方法
2015/05/07 Javascript
跟我学习javascript解决异步编程异常方案
2015/11/23 Javascript
基于Jquery和html5实现炫酷的3D焦点图动画
2016/03/02 Javascript
JS搜狐面试题分析
2016/12/16 Javascript
浅谈es6语法 (Proxy和Reflect的对比)
2017/10/24 Javascript
vue+springboot前后端分离实现单点登录跨域问题解决方法
2018/01/30 Javascript
在Vue组件中使用 TypeScript的方法
2018/02/28 Javascript
JS中的JSON对象的定义和取值实现代码
2018/05/09 Javascript
以v-model与promise两种方式实现vue弹窗组件
2018/05/21 Javascript
vue计算属性computed的使用方法示例
2019/03/13 Javascript
基于JS实现一个随机生成验证码功能
2019/05/29 Javascript
小程序input数据双向绑定实现方法
2019/10/17 Javascript
React实现评论的添加和删除
2020/10/20 Javascript
[10:28]2018DOTA2国际邀请赛寻真——VGJ.S寻梦之路
2018/08/15 DOTA
[32:39]完美世界DOTA2联赛循环赛 Forest vs Inki BO2第一场 11.04
2020/11/04 DOTA
python实现SMTP邮件发送功能
2020/06/16 Python
Python实现通讯录功能
2018/02/22 Python
Python装饰器模式定义与用法分析
2018/08/06 Python
用django设置session过期时间的方法解析
2019/08/05 Python
Python Pickle 实现在同一个文件中序列化多个对象
2019/12/30 Python
Python通过正则库爬取淘宝商品信息代码实例
2020/03/02 Python
P D PAOLA法国官网:西班牙著名的珠宝首饰品牌
2020/02/15 全球购物
工商管理专业应届生求职信
2013/11/04 职场文书
公司活动方案范文
2014/03/06 职场文书
幼儿园中班开学寄语
2014/04/03 职场文书
2015年党员承诺书
2015/01/21 职场文书
因个人工作失误检讨书
2019/06/21 职场文书
OpenCV实现普通阈值
2021/11/17 Java/Android
MySQL数据库如何给表设置约束详解
2022/03/13 MySQL