程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

python從數據庫導出電子表格

編輯:Python
from sqlalchemy import create_engine
import pandas as pd
# 創建數據庫連接
engine = create_engine('mysql://root:[email protected]/testdb')
# 讀取mysql數據
db = pd.read_sql(sql='select * from testdb.t1', con=engine)
# 導出數據到excel
db.to_excel('testExportData.xlsx')

mysql的連接字符串:

mysql://用戶:密碼@192.168.0.21/testdb

安裝依賴項目

pip install sqlalchemy

pip install pandas


pip install mysqlclient

Collecting mysqlclient
  Downloading mysqlclient-2.1.1-cp310-cp310-win_amd64.whl (178 kB)
     |████████████████████████████████| 178 kB 15 kB/s
Installing collected packages: mysqlclient
Successfully installed mysqlclient-2.1.1
WARNING: You are using pip version 21.1.2; however, version 22.2.1 is available.

pip install openpyxl

Collecting openpyxl
  Downloading openpyxl-3.0.10-py2.py3-none-any.whl (242 kB)
     |████████████████████████████████| 242 kB 94 kB/s
Collecting et-xmlfile
  Downloading et_xmlfile-1.1.0-py3-none-any.whl (4.7 kB)
Installing collected packages: et-xmlfile, openpyxl
Successfully installed et-xmlfile-1.1.0 openpyxl-3.0.10
WARNING: You are using pip version 21.1.2; however, version 22.2.1 is available

 

 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved