Python:如何从oracle中读取数据,然后写入Excel

时间:2017-05-14 10:09:12

标签: python excel oracle

Python:如何从oracle中读取数据,然后写入Excel,Excel的部分列只能读取?

3 个答案:

答案 0 :(得分:0)

安装database driver以连接数据库和pandas以简化Excel写入和数据操作。

$ pip install cx_Oracle pandas

脚本中的

执行以下操作:

import cx_Oracle

con = cx_Oracle.connect('user/password@dsn')

data = pd.read_sql('SELECT * FROM mytable', con)
con.close()

data.head() # take a peek at data
data.to_excel('my_oracle_table.xlsx')

答案 1 :(得分:0)

这个答案来自: Python xlwt - making a column readonly (cell protect)

from xlwt import Workbook, Worksheet, easyxf
# ...
# Protect worksheet - all cells will be read-only by default
my_worksheet.protect = True  # defaults to False
my_worksheet.password = "something_difficult_to_guess"

# Create cell styles for both read-only and editable cells
editable = easyxf("protection: cell_locked false;")
read_only = easyxf("")  # "cell_locked true" is default
# Apply your new styles when writing cells
my_worksheet.write(0, 0, "Can't touch this!", read_only)
my_worksheet.write(2, 2, "Erase me :)", editable)

答案 2 :(得分:0)

感谢Robin Nemeth的回答。我对其进行了一些修改,并获得了所需的输出。

import cx_Oracle
con = cx_Oracle.connect('user/password@dsn')
data = pd.read_sql('SELECT * FROM mytable', con)
data.to_excel('my_oracle_table.xlsx')  #called before closing the connection
con.close()

在此处,应在关闭连接之前调用data.to_excel(),否则将导致以下错误“ DatabaseError:DPI-1040:LOB已关闭”。

甚至删除了data.head()函数,该函数不会影响我的输出。