python實現(xiàn)的生成word文檔功能示例
本文實例講述了python實現(xiàn)的生成word文檔功能。分享給大家供大家參考,具體如下:
每月1次的測試費用報銷,需要做一個文檔。干脆花點時間寫個程序吧。
# -*- coding: utf-8 -*-
from tools import get_data
from docx import Document
def new_doc(fee_data,doc_path,fee):#新建一個word文檔,寫入?yún)R總表的數(shù)據(jù)
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'運(yùn)費'
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:#如果實付總額大于傳入的金額,終止寫入數(shù)據(jù),并記錄序號
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'測試訂單明細(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'訂單號:' + 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文件中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ù)庫帳號和密碼。
運(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文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
python爬蟲實戰(zhàn)steam加密逆向RSA登錄解析
今天帶來爬蟲實戰(zhàn)的文章。在挑選游戲的過程中感受學(xué)習(xí),讓你突飛猛進(jìn)。本文主要實現(xiàn)用Python逆向登錄世界上最大的游戲平臺源碼分享,了解steam加密手段有多高明2021-10-10
基于python實現(xiàn)鼠標(biāo)實時坐標(biāo)監(jiān)測
這篇文章主要給大家介紹了如何基于python實現(xiàn)鼠標(biāo)實時坐標(biāo)監(jiān)測,文章通過代碼示例介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-11-11
跟老齊學(xué)Python之使用Python操作數(shù)據(jù)庫(1)
本文詳細(xì)講述了使用python操作數(shù)據(jù)庫所需要了解的知識以及準(zhǔn)備工作,十分的詳盡,這里推薦給想學(xué)習(xí)python的小伙伴。2014-11-11
Python?FastApi結(jié)合異步執(zhí)行方式
這篇文章主要介紹了Python?FastApi結(jié)合異步執(zhí)行方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06
科學(xué)計算與數(shù)據(jù)分析利器Python數(shù)據(jù)分析庫Scipy使用詳解
Scipy在現(xiàn)代科學(xué)研究和數(shù)據(jù)分析中是一個不可或缺的庫,它建立在NumPy的基礎(chǔ)上,提供了更多的高級科學(xué)計算功能,包括優(yōu)化、信號處理、統(tǒng)計分析、插值、線性代數(shù)等,本文將會學(xué)習(xí)Scipy庫的各種功能和用法,包括數(shù)學(xué)優(yōu)化、統(tǒng)計分析、信號處理和插值等方面2023-11-11
Python爬蟲利用cookie實現(xiàn)模擬登陸實例詳解
這篇文章主要介紹了Python爬蟲利用cookie實現(xiàn)模擬登陸實例詳解的相關(guān)資料,需要的朋友可以參考下2017-01-01

