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创建XML文档
Mar 01 Python
python在linux中输出带颜色的文字的方法
Jun 19 Python
python使用自定义user-agent抓取网页的方法
Apr 15 Python
Python实现视频下载功能
Mar 14 Python
python中列表和元组的区别
Dec 18 Python
pandas DataFrame数据转为list的方法
Apr 11 Python
python爬虫爬取幽默笑话网站
Oct 24 Python
基于pytorch 预训练的词向量用法详解
Jan 06 Python
Python + selenium + crontab实现每日定时自动打卡功能
Mar 31 Python
python logging通过json文件配置的步骤
Apr 27 Python
python怎么删除缓存文件
Jul 19 Python
Python合并多张图片成PDF
Jun 09 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
Docker搭建自己的PHP开发环境
2018/02/24 PHP
JavaScript中链式调用之研习
2011/04/07 Javascript
jQuery判断元素是否显示 是否隐藏的简单实现代码
2016/05/19 Javascript
Javascript必知必会(四)js类型转换
2016/06/08 Javascript
js获取指定字符前/后的字符串简单实例
2016/10/27 Javascript
我要点爆”微信小程序云开发之项目建立与我的页面功能实现
2019/05/26 Javascript
antd Select下拉菜单动态添加option里的内容操作
2020/11/02 Javascript
Python中zip()函数用法实例教程
2014/07/31 Python
Eclipse中Python开发环境搭建简单教程
2016/03/23 Python
python executemany的使用及注意事项
2017/03/13 Python
Python 逐行分割大txt文件的方法
2017/10/10 Python
使用Python读取安卓手机的屏幕分辨率方法
2018/03/31 Python
python 用for循环实现1~n求和的实例
2019/02/01 Python
详解如何在cmd命令窗口中搭建简单的python开发环境
2019/08/29 Python
Python实现快速排序的方法详解
2019/10/25 Python
Node.js 和 Python之间该选择哪个?
2020/08/05 Python
Python hashlib模块的使用示例
2020/10/09 Python
工程造价管理专业大专生求职信
2013/10/06 职场文书
历史系毕业生自荐信
2013/10/28 职场文书
医学生自我鉴定范文
2013/11/08 职场文书
数据员岗位职责
2013/11/19 职场文书
二手房购房意向书范本
2014/04/01 职场文书
软件项目实施计划书
2014/05/02 职场文书
工作作风承诺书
2014/08/30 职场文书
2014年教师节国旗下讲话稿
2014/09/10 职场文书
师德师风自我评价范文
2014/09/11 职场文书
党员民主生活会个人整改措施材料
2014/09/16 职场文书
党的群众路线对照检查材料思想汇报(学校)
2014/10/04 职场文书
2014年社区教育工作总结
2014/12/02 职场文书
教师业务学习材料
2014/12/16 职场文书
胡桃夹子观后感
2015/06/11 职场文书
2015初一年级组工作总结
2015/07/24 职场文书
2019让人心动的商业计划书
2019/06/27 职场文书
熟背这些句子,让您的英语口语突飞猛进(135句)
2019/09/06 职场文书
MongoDB数据库常用的10条操作命令
2021/06/18 MongoDB
解析Redis Cluster原理
2021/06/21 Redis