欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實(shí)現(xiàn)的生成word文檔功能示例

 更新時(shí)間:2019年08月23日 11:50:26   作者:zhizunyu2009  
這篇文章主要介紹了python實(shí)現(xiàn)的生成word文檔功能,涉及Python數(shù)據(jù)查詢、遍歷及word文檔生成相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了python實(shí)現(xiàn)的生成word文檔功能。分享給大家供大家參考,具體如下:

每月1次的測試費(fèi)用報(bào)銷,需要做一個(gè)文檔。干脆花點(diǎn)時(shí)間寫個(gè)程序吧。

# -*- coding: utf-8 -*-
from tools import get_data
from docx import Document
def new_doc(fee_data,doc_path,fee):#新建一個(gè)word文檔,寫入?yún)R總表的數(shù)據(jù)
  document = Document()
  p_total = document.add_paragraph()
  r_total = p_total.add_run(u'測試訂單費(fèi)用匯總表:')
  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'序號(hào)'
  heading_cells[1].text = u'訂單號(hào)'
  heading_cells[2].text = u'訂單總額'
  heading_cells[3].text = u'運(yùn)費(fèi)'
  heading_cells[4].text = u'實(shí)付金額'
  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:#如果實(shí)付總額大于傳入的金額,終止寫入數(shù)據(jù),并記錄序號(hào)
      number = i
      break
  total = str(float(total)/100)
  document.add_paragraph(u'實(shí)付金額總計(jì):' + total + u' 元。')
  document.add_paragraph()
  p_detail = document.add_paragraph()
  r_detail = p_detail.add_run(u'測試訂單明細(xì):')
  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'訂單號(hào):' + order_no + u'實(shí)付金額:' + paid_amount + u'元。'
    document.add_paragraph(row_str)
  document.save(doc_path)
if __name__ == "__main__":
  #sql語句篩選實(shí)付金額在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文件中g(shù)et_data函數(shù)

# -*- coding: utf-8 -*-
import MySQLdb
import conf
def get_data(*sql_list):#根據(jù)sql語句,獲取數(shù)據(jù)庫的數(shù)據(jù)
  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文件中記錄的數(shù)據(jù)庫帳號(hào)和密碼。

運(yùn)行結(jié)果:

這里寫圖片描述

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論