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实现简单温度转换的方法
Mar 13 Python
python目录与文件名操作例子
Aug 28 Python
python操作MySQL 模拟简单银行转账操作
Sep 27 Python
Python遍历numpy数组的实例
Apr 04 Python
查看Django和flask版本的方法
May 14 Python
深入解析Python小白学习【操作列表】
Mar 23 Python
Python3 列表,数组,矩阵的相互转换的方法示例
Aug 05 Python
Python图像处理模块ndimage用法实例分析
Sep 05 Python
解决pycharm最左侧Tool Buttons显示不全的问题
Dec 17 Python
在python image 中实现安装中文字体
May 16 Python
python wsgiref源码解析
Feb 06 Python
使用Python webdriver图书馆抢座自动预约的正确方法
Mar 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
PHP访问数据库集群的方法小结
2016/03/14 PHP
Yii2超好用的日期和时间组件(值得收藏)
2016/05/05 PHP
php基于自定义函数记录log日志方法
2017/07/21 PHP
Jquery 快速构建可拖曳的购物车DragDrop
2009/11/30 Javascript
jquery图片延迟加载 前端开发技能必备系列
2012/06/18 Javascript
JSON辅助格式化处理方法
2013/03/26 Javascript
JavaScript事件委托的技术原理探讨示例
2014/04/17 Javascript
javascript if条件判断方法小结
2014/05/17 Javascript
Jquery基础教程之DOM操作
2015/08/19 Javascript
使用Nuxt.js改造已有项目的方法
2018/08/07 Javascript
vue中当图片地址无效的时候,显示默认图片的方法
2018/09/18 Javascript
微信小程序dom操作的替代思路实例分析
2018/12/06 Javascript
JS实现指定区域的全屏显示功能示例
2019/04/25 Javascript
在Express中提供静态文件的实现方法
2019/10/17 Javascript
js键盘事件实现人物的行走
2020/01/17 Javascript
vscode+gulp轻松开发小程序的完整步骤
2020/10/18 Javascript
[02:36]DOTA2混沌骑士 英雄基础教程
2013/11/26 DOTA
[51:36]EG vs VP 2018国际邀请赛淘汰赛BO3 第一场 8.24
2018/08/25 DOTA
Python常用随机数与随机字符串方法实例
2015/04/09 Python
python 异常处理总结
2016/10/18 Python
Python with语句上下文管理器两种实现方法分析
2018/02/09 Python
pandas string转dataframe的方法
2018/04/11 Python
python获取交互式ssh shell的方法
2019/02/14 Python
Django框架视图介绍与使用详解
2019/07/18 Python
如何用Anaconda搭建虚拟环境并创建Django项目
2020/08/02 Python
Django+Django-Celery+Celery的整合实战
2021/01/20 Python
基于注解实现 SpringBoot 接口防刷的方法
2021/03/02 Python
12月小学生校园广播稿
2014/02/04 职场文书
银行开业庆典方案
2014/02/06 职场文书
带病坚持工作事迹
2014/05/03 职场文书
市政管理求职信范文
2014/05/07 职场文书
研究生导师评语
2014/12/31 职场文书
销售员岗位职责范本
2015/04/11 职场文书
2016年推广普通话宣传周活动总结
2016/04/06 职场文书
导游词之无锡梅园
2019/11/28 职场文书
教你如何使用Python下载B站视频的详细教程
2021/04/29 Python