使用xlwings读取excel中所有的值,写入mysql

使用xlwings读取excel中所有的值,写入mysql

#导入xlwings模块,打开Excel程序,默认设置:程序可见,只打开不新建工作薄,屏幕更新关闭
import xlwings as xw
app=xw.App(visible=True,add_book=False)
app.display_alerts=True
app.screen_updating=True
#文件位置:filepath,打开test文档,然后保存,关闭,结束程序
filepath=r'C:\Users\gsd\Desktop\test1.xlsx'
wb=app.books.open(filepath)
sht=wb.sheets['sheet1']

a=sht.range('A2').expand().value



wb.save()
wb.close()
app.quit()

数据库操作

import pymysql.cursors

# 连接数据库
connect = pymysql.Connect(
    host='localhost',
    port=3306,
    user='root',
    passwd='111111',
    db='test',
    charset='utf8'
)

# 获取游标
cursor = connect.cursor()

# 插入数据
sql = "INSERT INTO trade (name, account,) VALUES (  '%s',' %s' );"
data = ','.join(str(i)for i in a)
print (data)
cursor.execute( sql % self._escape_args(args, conn))
connect.commit()
print('成功插入', cursor.rowcount, '条数据')

你可能感兴趣的:(Python)