服务器环境:Centos7,系统自带python版本:2.7.5
python第三方模块安装
MySQLdb安装:
版本选择:1.2.5,下载地址:https://files.pythonhosted.org/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip (源码包【csdn资源】)
下载后,解压并进入MySQL-python-1.2.5
执行命令:
python setup.py build
python setup.py install
出现下列错误:
error: command 'gcc' failed with exit status 1
解决方法:
yum install make automake gcc gcc-c++ kernel-devel
yum install python-devel
openpyxl安装:
版本选择:2.6.4
执行命令:pip install openpyxl==2.6.4
脚本准备:
#encoding:utf-8
from decimal import Decimal
import MySQLdb as db
import openpyxl
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
conn = db.connect("***","***","***","***",charset='utf8')
cursor = conn.cursor()
wb = openpyxl.Workbook()
sheet = wb.worksheets[0]
sheet.cell(1, 1, "订单号")
sheet.cell(1, 2, "入账日期")
sheet.cell(1, 3, "入账金额")
sheet.cell(1, 4, "商户名称")
cursor.execute("sql")
rows = cursor.fetchall()
conn.close()
i = 2
for row in rows:
money = Decimal(row[2])
sheet.cell(i, 1, row[0])
sheet.cell(i, 2, row[1])
sheet.cell(i, 3, money)
sheet.cell(i, 4, row[3])
i = i+1
wb.save("*.xlsx")
wb.close()
脚本执行:
python 脚本.py