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使用htpasswd实现基本认证授权的例子
Jun 10 Python
一个超级简单的python web程序
Sep 11 Python
Python 2.7.x 和 3.x 版本的重要区别小结
Nov 28 Python
python读取excel表格生成erlang数据
Aug 26 Python
浅谈python 线程池threadpool之实现
Nov 17 Python
Python基于更相减损术实现求解最大公约数的方法
Apr 04 Python
python实现求两个字符串的最长公共子串方法
Jul 20 Python
Django使用AJAX调用自己写的API接口的方法
Mar 06 Python
Django 用户认证组件使用详解
Jul 23 Python
100行Python代码实现每天不同时间段定时给女友发消息
Sep 27 Python
python利用dlib获取人脸的68个landmark
Nov 27 Python
python利用蒙版抠图(使用PIL.Image和cv2)输出透明背景图
Aug 04 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
将文件夹压缩成zip文件的php代码
2009/12/14 PHP
简单的php文件上传(实例)
2013/10/27 PHP
Codeigniter整合Tank Auth权限类库详解
2014/06/12 PHP
Laravel中扩展Memcached缓存驱动实现使用阿里云OCS缓存
2015/02/10 PHP
php打造智能化的柱状图程序,用于报表等
2015/06/19 PHP
php随机获取金山词霸每日一句的方法
2015/07/09 PHP
ThinkPHP2.x防范XSS跨站攻击的方法
2015/09/25 PHP
php安装dblib扩展,连接mssql的具体步骤
2017/03/02 PHP
thinkPHP利用ajax异步上传图片并显示、删除的示例
2018/09/26 PHP
Mootools 1.2 手风琴(Accordion)教程
2009/09/15 Javascript
ExtJs grid行 右键菜单的两种方法
2010/06/19 Javascript
JS 跳转页面延迟2种方法
2013/03/29 Javascript
setInterval与clearInterval的使用示例代码
2014/01/28 Javascript
javascript写的异步加载js文件函数(支持数组传参)
2014/06/07 Javascript
node.js中的console.dir方法使用说明
2014/12/10 Javascript
js实现多图左右切换功能
2016/08/04 Javascript
JavaScript requestAnimationFrame动画详解
2017/09/14 Javascript
js统计页面上每个标签的数量实例代码
2018/05/29 Javascript
手淘flexible.js框架使用和源代码讲解小结
2018/10/15 Javascript
vue项目打包之后背景样式丢失的解决方案
2019/01/17 Javascript
详解javascript中的Error对象
2019/04/25 Javascript
在js文件中引入(调用)另一个js文件的三种方法
2020/09/11 Javascript
Python之PyUnit单元测试实例
2014/10/11 Python
Python的爬虫程序编写框架Scrapy入门学习教程
2016/07/02 Python
使用pandas中的DataFrame数据绘制柱状图的方法
2018/04/10 Python
Django 内置权限扩展案例详解
2019/03/04 Python
Python批量删除只保留最近几天table的代码实例
2019/04/01 Python
Python学习笔记之变量、自定义函数用法示例
2019/05/28 Python
Python如何输出整数
2020/06/07 Python
自考生自我鉴定范文
2013/10/01 职场文书
九年级数学教学反思
2014/02/02 职场文书
国际会计专业求职信
2014/08/04 职场文书
党的群众路线对照检查材料思想汇报
2014/09/25 职场文书
导游欢送词
2015/01/31 职场文书
Python中zipfile压缩包模块的使用
2021/05/14 Python
css3中2D转换之有趣的transform形变效果
2022/02/24 HTML / CSS