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中用PIL库批量给图片加上序号的教程
May 06 Python
Python的Django框架中settings文件的部署建议
May 30 Python
使用简单工厂模式来进行Python的设计模式编程
Mar 01 Python
Python编写登陆接口的方法
Jul 10 Python
Python实现确认字符串是否包含指定字符串的实例
May 02 Python
python基础学习之如何对元组各个元素进行命名详解
Jul 12 Python
python语言元素知识点详解
May 15 Python
Python画图高斯分布的示例
Jul 10 Python
使用PyInstaller将Pygame库编写的小游戏程序打包为exe文件及出现问题解决方法
Sep 06 Python
python使用PIL和matplotlib获取图片像素点并合并解析
Sep 10 Python
常用python爬虫库介绍与简要说明
Jan 25 Python
Python bytes string相互转换过程解析
Mar 05 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
中国收音机工业发展史
2021/03/02 无线电
轻松入门: 煮好咖啡的七个诀窍
2021/03/03 冲泡冲煮
十天学会php之第四天
2006/10/09 PHP
PHP调用MySQL的存储过程的实现代码
2008/08/12 PHP
php strstr查找字符串中是否包含某些字符的查找函数
2010/06/03 PHP
解析thinkphp import 文件内容变量失效的问题
2013/06/20 PHP
使用PHP连接多种数据库的实现代码(mysql,access,sqlserver,Oracle)
2016/12/21 PHP
php使用mysqli和pdo扩展,测试对比mysql数据库的执行效率完整示例
2019/05/09 PHP
JavaScript Event学习第二章 Event浏览器兼容性
2010/02/07 Javascript
jQuery使用之处理页面元素用法实例
2015/01/19 Javascript
jQuery改变form表单的action,并进行提交的实现代码
2016/05/25 Javascript
JavaScript算法系列之快速排序(Quicksort)算法实例详解
2016/09/04 Javascript
AngularJS自定义插件实现网站用户引导功能示例
2016/11/07 Javascript
详解react-redux插件入门
2018/04/19 Javascript
基于jQuery实现无缝轮播与左右点击效果
2018/05/13 jQuery
解决ng-repeat产生的ng-model中取不到值的问题
2018/10/02 Javascript
JavaScript实现单图片上传并预览功能
2019/09/30 Javascript
纯js实现无缝滚动功能代码实例
2020/02/21 Javascript
支付宝小程序实现省市区三级联动
2020/06/21 Javascript
Python中使用logging模块代替print(logging简明指南)
2014/07/09 Python
requests和lxml实现爬虫的方法
2017/06/11 Python
Python返回数组/List长度的实例
2018/06/23 Python
Python及Pycharm安装方法图文教程
2019/08/05 Python
如何基于python测量代码运行时间
2019/12/25 Python
五种Python转义表示法
2020/11/27 Python
德国箱包网上商店:koffer24.de
2016/07/27 全球购物
澳大利亚领先的折扣药房:Chemist Direct(有中文站)
2018/11/24 全球购物
Tod’s英国官方网站:意大利奢华手工制作手袋和鞋履
2019/03/15 全球购物
计算机应用专业学生的自我评价分享
2013/11/03 职场文书
甲方资料员岗位职责
2013/12/13 职场文书
《宋庆龄故居的樟树》教学反思
2014/04/07 职场文书
集中采购方案
2014/06/10 职场文书
课外访万家心得体会
2014/09/03 职场文书
2014年语文教研组工作总结
2014/12/06 职场文书
高一英语教学反思
2016/03/03 职场文书
关于Python使用turtle库画任意图的问题
2022/04/01 Python