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的Django框架中更新数据库数据的方法
Jul 17 Python
Python实现二叉堆
Feb 03 Python
Python爬虫通过替换http request header来欺骗浏览器实现登录功能
Jan 07 Python
python实现识别手写数字 python图像识别算法
Mar 23 Python
Django uwsgi Nginx 的生产环境部署详解
Feb 02 Python
Python中Numpy ndarray的使用详解
May 24 Python
简单了解python 邮件模块的使用方法
Jul 24 Python
Python中利用LSTM模型进行时间序列预测分析的实现
Jul 26 Python
pytorch方法测试——激活函数(ReLU)详解
Jan 15 Python
jupyter notebook实现显示行号
Apr 13 Python
python使用多线程查询数据库的实现示例
Aug 17 Python
Python使用PyYAML库读写yaml文件的方法
Apr 06 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
本地机apache配置基于域名的虚拟主机详解
2013/08/10 PHP
CI框架在CLI下执行占用内存过大问题的解决方法
2014/06/17 PHP
php 解决substr()截取中文字符乱码问题
2016/07/18 PHP
拖动Html元素集合 Drag and Drop any item
2006/12/22 Javascript
javascript下操作css的float属性的特殊写法
2007/08/22 Javascript
JavaScript高级程序设计 DOM学习笔记
2011/09/10 Javascript
jQuery 过滤not()与filter()实例代码
2012/05/10 Javascript
关于jQuery UI 使用心得及技巧
2012/10/10 Javascript
基于jQuery中对数组进行操作的方法
2013/04/16 Javascript
javascript实现瀑布流自适应遇到的问题及解决方案
2015/01/28 Javascript
使用node+vue.js实现SPA应用
2016/01/28 Javascript
require.js配合插件text.js实现最简单的单页应用程序
2016/07/12 Javascript
Angularjs中使用layDate日期控件示例
2017/01/11 Javascript
vue监听scroll的坑的解决方法
2017/09/07 Javascript
js 判断一个数字是不是2的n次方幂的实例
2017/11/26 Javascript
使用Vuex实现一个笔记应用的方法
2018/03/13 Javascript
Javascript Web Worker使用过程解析
2020/03/16 Javascript
vue引入静态js文件的方法
2020/06/20 Javascript
vue render函数动态加载img的src路径操作
2020/10/26 Javascript
用Python写的图片蜘蛛人代码
2012/08/27 Python
对于Python的Django框架部署的一些建议
2015/04/09 Python
python实现的jpg格式图片修复代码
2015/04/21 Python
python迭代器与生成器详解
2016/03/10 Python
Python爬虫框架scrapy实现downloader_middleware设置proxy代理功能示例
2018/08/04 Python
Python使用pandas对数据进行差分运算的方法
2018/12/22 Python
python使用PIL和matplotlib获取图片像素点并合并解析
2019/09/10 Python
浅谈ROC曲线的最佳阈值如何选取
2020/02/28 Python
jupyter notebook 参数传递给shell命令行实例
2020/04/10 Python
华为c/c++笔试题
2016/01/25 面试题
语文教育专业推荐信范文
2013/11/25 职场文书
党员公开承诺事项
2014/03/25 职场文书
三年级班级文化建设方案
2014/05/04 职场文书
九年级化学教学反思
2016/02/22 职场文书
win10安装配置nginx的过程
2021/03/31 Servers
JavaScript 数组去重详解
2021/09/15 Javascript
利用python做数据拟合详情
2021/11/17 Python