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实现的抓取腾讯视频所有电影的爬虫
Apr 22 Python
Python3+django2.0+apache2+ubuntu14部署网站上线的方法
Jul 07 Python
浅析python的Lambda表达式
Feb 27 Python
详解python算法之冒泡排序
Mar 05 Python
我们为什么要减少Python中循环的使用
Jul 10 Python
在vscode中配置python环境过程解析
Sep 28 Python
pycharm 对代码做静态检查操作
Jun 09 Python
Python paramiko使用方法代码汇总
Nov 20 Python
使用Python封装excel操作指南
Jan 29 Python
python基础学习之生成器与文件系统知识总结
May 25 Python
Python尝试实现蒙特卡罗模拟期权定价
Apr 21 Python
Python+pyaudio实现音频控制示例详解
Jul 23 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
10个可以简化php开发过程的MySQL工具
2010/04/11 PHP
PHP的foreach中使用引用时需要注意的一个问题和解决方法
2014/05/29 PHP
php使用array_search函数实现数组查找的方法
2015/06/12 PHP
PHP响应post请求上传文件的方法
2015/12/17 PHP
PHP6连接SQLServer2005的三部曲
2016/04/15 PHP
利用PHP命令行模式采集股票趋势信息
2016/08/09 PHP
ThinkPHP 整合Bootstrap Ajax分页样式
2016/12/23 PHP
客户端 使用XML DOM加载json数据的方法
2010/09/28 Javascript
js 判断js函数、变量是否存在的简单示例代码
2014/03/04 Javascript
基于jquery实现的可编辑下拉框实现代码
2014/08/02 Javascript
PHP+jQuery实现随意拖动层并即时保存拖动位置
2015/04/30 Javascript
微信小程序 时间格式化(util.formatTime(new Date))详解
2016/11/16 Javascript
Node.js连接MongoDB数据库产生的问题
2017/02/08 Javascript
关于jQuery中fade(),show()起始位置的一点小发现
2017/04/25 jQuery
JS+H5 Canvas实现时钟效果
2018/07/20 Javascript
vue 刷新之后 嵌套路由不变 重新渲染页面的方法
2018/09/13 Javascript
jQuery 点击获取验证码按钮及倒计时功能
2018/09/20 jQuery
vue代码分割的实现(codesplit)
2018/11/13 Javascript
vue响应式更新机制及不使用框架实现简单的数据双向绑定问题
2019/06/27 Javascript
Javascript ParentNode和ChildNode接口原理解析
2020/03/16 Javascript
[35:44]2014 DOTA2华西杯精英邀请赛 5 24 iG VS VG
2014/05/26 DOTA
python简单操作excle的方法
2018/09/12 Python
对Python的zip函数妙用,旋转矩阵详解
2018/12/13 Python
PyQt5 QListWidget选择多项并返回的实例
2019/06/17 Python
python对XML文件的操作实现代码
2020/03/27 Python
python退出循环的方法
2020/06/18 Python
世界上最大的在线旅行社新加坡网站:Expedia新加坡
2016/08/25 全球购物
Pat McGrath Labs官网:世界上最有影响力的化妆师推出的彩妆品牌
2018/01/07 全球购物
英国独特的时尚和生活方式品牌:JOY
2018/03/17 全球购物
Made in Design德国:设计师家具、灯具和装饰
2019/10/31 全球购物
马来西亚在线药房:RoyalePharma
2019/12/01 全球购物
澳大利亚领先的男装零售连锁店:Lowes
2020/08/07 全球购物
面料业务员岗位职责
2013/12/26 职场文书
甜点店创业计划书
2014/01/27 职场文书
妇女儿童发展规划实施方案
2014/03/16 职场文书
大足石刻导游词
2015/02/02 职场文书