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


Posted in Python onAugust 23, 2019

本文实例讲述了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实现的生成word文档功能示例

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

Python 相关文章推荐
Python 字符串定义
Sep 25 Python
Python代码的打包与发布详解
Jul 30 Python
Python存取XML的常见方法实例分析
Mar 21 Python
Python OpenCV 直方图的计算与显示的方法示例
Feb 08 Python
Python中跳台阶、变态跳台阶与矩形覆盖问题的解决方法
May 19 Python
python去除拼音声调字母,替换为字母的方法
Nov 28 Python
python面向对象入门教程之从代码复用开始(一)
Dec 11 Python
Python如何使用k-means方法将列表中相似的句子归类
Aug 08 Python
对python while循环和双重循环的实例详解
Aug 23 Python
简单了解python装饰器原理及使用方法
Dec 18 Python
调整Jupyter notebook的启动目录操作
Apr 10 Python
详解Python如何批量采集京东商品数据流程
Jan 22 Python
Python实现微信中找回好友、群聊用户撤回的消息功能示例
Aug 23 #Python
详解Matplotlib绘图之属性设置
Aug 23 #Python
python3.6生成器yield用法实例分析
Aug 23 #Python
python基础 range的用法解析
Aug 23 #Python
Django 导出项目依赖库到 requirements.txt过程解析
Aug 23 #Python
Django接收自定义http header过程详解
Aug 23 #Python
Python 处理文件的几种方式
Aug 23 #Python
You might like
PHP使用curl函数发送Post请求的注意事项
2016/11/26 PHP
PHP读取目录树的实现方法分析
2019/03/22 PHP
详解php反序列化
2020/06/10 PHP
在IE6下发生Internet Explorer cannot open the Internet site错误
2010/06/21 Javascript
如何确保JavaScript的执行顺序 之jQuery.html并非万能钥匙
2011/03/03 Javascript
Jquery上传插件 uploadify v3.1使用说明
2012/06/18 Javascript
jquery js 重置表单 reset()具体实现代码
2013/08/05 Javascript
javascript日期计算实例分析
2015/06/29 Javascript
jQuery实现购物车表单自动结算效果实例
2015/08/10 Javascript
使用JS中的exec()方法构造正则表达式验证
2016/08/01 Javascript
Jquery-data的三种用法
2017/04/18 jQuery
基于AngularJS的拖拽文件上传的实例代码
2017/07/15 Javascript
php main 与 iframe 相互通讯类(js+php同域/跨域)
2017/09/14 Javascript
JavaScript实现HTML5游戏断线自动重连的方法
2017/09/18 Javascript
教你用Cordova打包Vue项目的方法
2017/10/17 Javascript
详解Vue基于vue-quill-editor富文本编辑器使用心得
2019/01/03 Javascript
vue中音频wavesurfer.js的使用方法
2020/02/20 Vue.js
[03:34]2014DOTA2西雅图国际邀请赛 淘汰赛7月15日TOPPLAY
2014/07/15 DOTA
[57:29]Alliance vs KG 2019国际邀请赛小组赛 BO2 第二场 8.16
2019/08/17 DOTA
Python实现简单登录验证
2016/04/13 Python
python中的lambda表达式用法详解
2016/06/22 Python
Python装饰器原理与简单用法实例分析
2018/04/29 Python
Python使用sort和class实现的多级排序功能示例
2018/08/15 Python
浅谈python的dataframe与series的创建方法
2018/11/12 Python
python 使用pandas计算累积求和的方法
2019/02/08 Python
python爬虫 线程池创建并获取文件代码实例
2019/09/28 Python
pyqt5 textEdit、lineEdit操作的示例代码
2020/08/12 Python
解决pytorch 的state_dict()拷贝问题
2021/03/03 Python
HTML5+css3:3D旋转木马效果相册
2017/01/03 HTML / CSS
KIKO美国官网:意大利的平价彩妆品牌
2017/05/16 全球购物
女子锻炼服装和瑜伽服装:Splits59
2019/03/04 全球购物
护理职业生涯规划书
2014/01/24 职场文书
2015年教务工作总结
2015/05/23 职场文书
《检阅》教学反思
2016/02/22 职场文书
Golang标准库syscall详解(什么是系统调用)
2021/05/25 Golang
Python面向对象编程之类的概念
2021/11/01 Python