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获取当前计算机cpu数量的方法
Apr 18 Python
在Python中使用SQLite的简单教程
Apr 29 Python
python通过get,post方式发送http请求和接收http响应的方法
May 26 Python
python模拟Django框架实例
May 17 Python
Python3.4编程实现简单抓取爬虫功能示例
Sep 14 Python
python2 与python3的print区别小结
Jan 16 Python
用TensorFlow实现多类支持向量机的示例代码
Apr 28 Python
Django model update的多种用法介绍
Mar 28 Python
python如何实现不用装饰器实现登陆器小程序
Dec 14 Python
jenkins+python自动化测试持续集成教程
May 12 Python
详解基于Facecognition+Opencv快速搭建人脸识别及跟踪应用
Jan 21 Python
pytest fixtures装饰器的使用和如何控制用例的执行顺序
Jan 28 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
WINDOWS下php5.2.4+mysql6.0+apache2.2.4+ZendOptimizer-3.3.0配置
2008/03/28 PHP
ThinkPHP的模版中调用session数据的方法
2014/07/01 PHP
推荐一款PHP+jQuery制作的列表分页的功能模块
2014/10/14 PHP
通过php删除xml文档内容的方法
2015/01/23 PHP
PHP使用反射机制实现查找类和方法的所在位置
2016/04/22 PHP
win10环境PHP 7 安装配置【教程】
2016/05/09 PHP
基于PHPexecl类生成复杂的报表表头示例
2016/10/14 PHP
php运行报错Call to undefined function curl_init()的最新解决方法
2016/11/20 PHP
thinkPHP框架实现的简单计算器示例
2018/12/07 PHP
PHP程序员简单的开展服务治理架构操作详解(二)
2020/05/14 PHP
基于PHP实现生成随机水印图片
2020/12/09 PHP
模仿JQuery.extend函数扩展自己对象的js代码
2009/12/09 Javascript
自定义右键属性覆盖浏览器默认右键行为实现代码
2013/02/02 Javascript
使用documentElement正确取得当前可见区域的大小
2014/07/25 Javascript
用JavaScript实现页面重定向功能的教程
2015/06/04 Javascript
在其他地方你学不到的jQuery小贴士和技巧(欢迎收藏)
2016/01/20 Javascript
浅析jQuery 3.0中的Data
2016/06/14 Javascript
Node.js的特点详解
2017/02/03 Javascript
xmlplus组件设计系列之路由(ViewStack)(7)
2017/05/02 Javascript
Node.js使用orm2进行update操作时关联字段无法修改的解决方法
2017/06/13 Javascript
AngularJS通过ng-Img-Crop实现头像截取的示例
2017/08/17 Javascript
swiper4实现移动端导航切换
2020/10/16 Javascript
Python MD5文件生成码
2009/01/12 Python
Python中优化NumPy包使用性能的教程
2015/04/23 Python
python爬取酷狗音乐排行榜
2019/02/20 Python
python实现逆滤波与维纳滤波示例
2020/02/26 Python
记一次django内存异常排查及解决方法
2020/08/07 Python
python使用bs4爬取boss直聘静态页面
2020/10/10 Python
美国最顶级的精品店之一:Hampden Clothing
2016/12/22 全球购物
捷克移动配件网上商店:ProMobily.cz
2019/03/15 全球购物
德国专业木制品经销商:Holz-Direkt24
2019/12/26 全球购物
六道php面试题附答案
2014/06/05 面试题
应届生体育教师自荐信
2013/10/03 职场文书
2014年乡镇团委工作总结
2014/12/18 职场文书
我的暑假生活作文(五年级)范文
2019/08/07 职场文书
Vue3如何理解ref toRef和toRefs的区别
2022/02/18 Vue.js