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中的split()函数的使用方法
Apr 07 Python
Python实现处理管道的方法
Jun 04 Python
python计算一个序列的平均值的方法
Jul 11 Python
Python for Informatics 第11章之正则表达式(二)
Apr 21 Python
wx.CheckBox创建复选框控件并响应鼠标点击事件
Apr 25 Python
Python基于OpenCV库Adaboost实现人脸识别功能详解
Aug 25 Python
python去除拼音声调字母,替换为字母的方法
Nov 28 Python
12个Python程序员面试必备问题与答案(小结)
Jun 24 Python
Python队列RabbitMQ 使用方法实例记录
Aug 05 Python
Python如何基于rsa模块实现非对称加密与解密
Jan 03 Python
详解Pycharm安装及Django安装配置指南
Sep 15 Python
手残删除python之后的补救方法
Jun 26 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
jQuery EasyUI API 中文文档 - MenuButton菜单按钮使用介绍
2011/10/06 Javascript
浅析jQuery1.8的几个小变化
2013/12/10 Javascript
Jquery获取元素的父容器对象示例代码
2014/02/10 Javascript
JSON格式的键盘编码对照表
2015/01/29 Javascript
jQuery基于ajax实现带动画效果无刷新柱状图投票代码
2015/08/10 Javascript
Bootstrap轮播加上css3动画,炫酷到底!
2015/12/22 Javascript
JS实现动画兼容性的transition和transform实例分析
2016/12/13 Javascript
JavaScript转换数据库DateTime字段类型方法
2017/06/27 Javascript
浅谈bootstrap layer.open中end的使用方法
2019/09/12 Javascript
Vue实现星级评价效果实例详解
2019/12/30 Javascript
Vue 解决通过this.$refs来获取DOM或者组件报错问题
2020/07/28 Javascript
微信小程序实现简单的select下拉框
2020/11/23 Javascript
js实现简单图片拖拽效果
2021/02/22 Javascript
用Python展示动态规则法用以解决重叠子问题的示例
2015/04/02 Python
Eclipse和PyDev搭建完美Python开发环境教程(Windows篇)
2016/11/16 Python
Python中支持向量机SVM的使用方法详解
2017/12/26 Python
python线程池threadpool使用篇
2018/04/27 Python
tensorflow更改变量的值实例
2018/07/30 Python
python爱心表白 每天都是浪漫七夕!
2018/08/18 Python
如何通过雪花算法用Python实现一个简单的发号器
2019/07/03 Python
django2笔记之路由path语法的实现
2019/07/17 Python
python/Matplotlib绘制复变函数图像教程
2019/11/21 Python
Python对wav文件的重采样实例
2020/02/25 Python
如何对python的字典进行排序
2020/06/19 Python
简述python&pytorch 随机种子的实现
2020/10/07 Python
django使用channels实现通信的示例
2020/10/19 Python
Amara德国:家居饰品、设计师品牌和豪华礼品
2019/05/20 全球购物
新闻专业本科生的自我评价分享
2013/11/20 职场文书
环保倡议书
2014/04/14 职场文书
公司门卫岗位职责范本
2014/07/08 职场文书
十佳标兵事迹材料
2014/08/18 职场文书
乡镇党委书记第三阶段个人整改措施
2014/09/16 职场文书
2014年个人工作总结范文
2014/11/07 职场文书
自主招生自荐信格式范文
2015/03/25 职场文书
2016年万圣节活动个人总结
2016/04/05 职场文书
Go语言的协程上下文的几个方法和用法
2022/04/11 Golang