首先是安装python-docx:(centos环境)

pip  install python-docx

基本方法使用:

from  docx import  Document
from docx.shared import Pt
from docx.oxml.ns import qn
from docx.shared import Inches

#打开文档
document = Document()

#加入不同等级的标题
document.add_heading('Document Title',0)
document.add_heading(u'二级标题',1)
document.add_heading(u'二级标题',2)

#添加文本
paragraph = document.add_paragraph(u'添加了文本')

#设置字号
run = paragraph.add_run(u'设置字号')
run.font.size=Pt(24)

#设置字体
run = paragraph.add_run('Set Font,')
run.font.name='Consolas'
#设置中文字体
run = paragraph.add_run(u'设置中文字体,')
run.font.name=u'宋体'
r = run._element
r.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')

#设置斜体
run = paragraph.add_run(u'斜体、')
run.italic = True

#设置粗体
run = paragraph.add_run(u'粗体').bold = True

#增加引用
document.add_paragraph('Intense quote', style='Intense Quote')

#增加有序列表
document.add_paragraph(
u'有序列表元素1',style='List Number'
)
document.add_paragraph(
u'有序列别元素2',style='List Number'
)

#增加无序列表
document.add_paragraph(
u'无序列表元素1',style='List Bullet'
)
document.add_paragraph(
u'无序列表元素2',style='List Bullet'
)

#增加图片(此处使用相对位置)
document.add_picture('jdb.jpg',width=Inches(1.25))

#增加表格
table = document.add_table(rows=3,cols=3)
hdr_cells=table.rows[0].cells
hdr_cells[0].text="第一列"
hdr_cells[1].text="第二列"
hdr_cells[2].text="第三列" hdr_cells = table.rows[1].cells
hdr_cells[0].text = ''
hdr_cells[1].text = 'aerszvfdgx'
hdr_cells[2].text = 'abdzfgxfdf' hdr_cells = table.rows[2].cells
hdr_cells[0].text = ''
hdr_cells[1].text = 'cafdwvaef'
hdr_cells[2].text = 'aabs zfgf'

#增加分页
document.add_page_break()
#保存文件
document.save('demo.docx')

效果展示:

 
 
 
 
 
下面是一小段代码,实现循环写入当前目录里的图片
 
 

最新文章

  1. websocket业务代码
  2. 在Excel中使用SQL语句查询和筛选
  3. PHP使用数组依次替换字符串中匹配项
  4. plist中的中文数据
  5. @RequestBody接收ajax的json字符串
  6. Arcengine 中,创建色带
  7. CGAffineTransform方法汇总
  8. [codevs1557]热浪
  9. MySQL 查询数据
  10. nodejs 记入
  11. 编写高质量代码改善程序的157个建议:第87个建议之区分WPF和WinForm的线程模型
  12. Apache启动不了httpd: apr_sockaddr_info_get() failed xgp
  13. 乙方渗透测试之Fuzz爆破
  14. Nginx负载均衡的4种方式 :轮询-Round Robin 、Ip地址-ip_hash、最少连接-least_conn、加权-weight=n
  15. 012_py之证书过期监测及域名使用的py列表的并集差集交集
  16. 23 python初学(模块和包)
  17. 使用 pandas 导出数据
  18. 使用vlfeat 包中遇到的问题
  19. 为docker私有registry配置nginx反向代理
  20. Vim 多行剪切、复制和删除

热门文章

  1. Django中间件限制用户访问频率
  2. zookpeer的安装与配置
  3. ubuntu apt-get用法
  4. oracle使用exp/imp导入导出(用户)
  5. codeforce453DIV2——D. GCD of Polynomials
  6. Codeforces 1120D (树形DP 或 最小生成树)
  7. python中Dict与OrderedDict
  8. 微信小程序怎么获取用户输入
  9. 23-单词数(HDU2070)
  10. 如何从Win7上卸载Sql 2008 R2 Express,再重装