python实现的生成word文档功能示例

yipeiwu_com5年前Python基础

本文实例讲述了python实现的生成word文档功能。分享给大家供大家参考,具体如下:

每月1次的测试费用报销,需要做一个文档。干脆花点时间写个程序吧。

# -*- coding: utf-8 -*-
from tools import get_data
from docx import Document
def new_doc(fee_data,doc_path,fee):#新建一个word文档,写入汇总表的数据
  document = Document()
  p_total = document.add_paragraph()
  r_total = p_total.add_run(u'测试订单费用汇总表:')
  r_total.font.bold = True
  table = document.add_table(1,5,style="Light List Accent 5")
  heading_cells = table.rows[0].cells
  heading_cells[0].text = u'序号'
  heading_cells[1].text = u'订单号'
  heading_cells[2].text = u'订单总额'
  heading_cells[3].text = u'运费'
  heading_cells[4].text = u'实付金额'
  total = 0
  for i in range(0,len(fee_data)):
    cells = table.add_row().cells
    cells[0].text = str(i+1)
    cells[1].text = str(fee_data[i][0])
    cells[2].text = str(float(fee_data[i][1])/100)
    cells[3].text = str(float(fee_data[i][2])/100)
    cells[4].text = str(float(fee_data[i][3])/100)
    total = total + fee_data[i][3]
    if total > fee:#如果实付总额大于传入的金额,终止写入数据,并记录序号
      number = i
      break
  total = str(float(total)/100)
  document.add_paragraph(u'实付金额总计:' + total + u' 元。')
  document.add_paragraph()
  p_detail = document.add_paragraph()
  r_detail = p_detail.add_run(u'测试订单明细:')
  r_detail.font.bold = True
  for i in range(0,number+1):
    order_no = str(fee_data[i][0])
    paid_amount = str(float(fee_data[i][3])/100)
    row_str = str(i+1) + '.' + u'订单号:' + order_no + u'实付金额:' + paid_amount + u'元。'
    document.add_paragraph(row_str)
  document.save(doc_path)
if __name__ == "__main__":
  #sql语句筛选实付金额在5元和39元之间的订单
  sql = "SELECT outer_order_id,order_amount,real_shipping_amount,paid_amount FROM oh_order_info WHERE " \
   "order_create_time between '2017-12-01 9:00:00' and '2017-12-27 9:00:00' " \
   "AND paid_amount between '500' and '3900'"
  fee_data = get_data(sql)
  doc_path = r'd:\yuzhong.docx'
  fee = 12300 #多少元以上,单位:分
  new_doc(fee_data,doc_path,fee)

使用到的tools文件中get_data函数

# -*- coding: utf-8 -*-
import MySQLdb
import conf
def get_data(*sql_list):#根据sql语句,获取数据库的数据
  conn = MySQLdb.connect(conf.test_dbhost,conf.test_user,conf.test_passd,conf.test_dbname,port=3306,charset="utf8")
  cur = conn.cursor()
  for sql in sql_list:
    cur.execute(sql)
  conn.commit()
  results = cur.fetchall()
  cur.close()
  conn.close()
  return results

conf文件中记录的数据库帐号和密码。

运行结果:

这里写图片描述

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

相关文章

今天 平安夜 Python 送你一顶圣诞帽 @微信官方

今天 平安夜 Python 送你一顶圣诞帽 @微信官方

还有多少耿直boy和我一样在等待微信官方送上一顶圣诞帽? 最后知道真相的我眼泪掉下来…… (还蒙在鼓里的同学请在微信最上方的搜索栏自行搜索『圣诞帽』) 好吧,你不给,咱自己来,不就...

简单了解Python中的几种函数

几个特殊的函数(待补充) python是支持多种范型的语言,可以进行所谓函数式编程,其突出体现在有这么几个函数: filter、map、reduce、lambda、yield lamb...

Python函数式编程指南:对生成器全面讲解

生成器是迭代器,同时也并不仅仅是迭代器,不过迭代器之外的用途实在是不多,所以我们可以大声地说:生成器提供了非常方便的自定义迭代器的途径。 这是函数式编程指南的最后一篇,似乎拖了一个星期才...

python中seaborn包常用图形使用详解

python中seaborn包常用图形使用详解

seaborn包是对matplotlib的增强版,需要安装matplotlib后才能使用。 所有图形都用plt.show()来显示出来,也可以使用下面的创建画布 fig,ax=plt...

flask中使用蓝图将路由分开写在不同文件实例解析

flask中使用蓝图将路由分开写在不同文件实例解析

本文的内容主要是flask中使用蓝图将路由分开写在不同文件的相关介绍,具体如下。 Flask 用 蓝图(blueprints) 的概念来在一个应用中或跨应用制作应用组件和支持通用的模式。...