Python: how to read the data from the oracle, and then write to Excel, Excel part of the column can be read only?
Asked
Active
Viewed 6,923 times
4 Answers
1
Install database driver for connecting to the db and pandas to streamline the excel writing and data manipulations.
$ pip install cx_Oracle pandas
in your script do something like this:
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')

redacted
- 3,789
- 6
- 25
- 38
-
I got it what you have showed,but I don't know how to set Excel part of the column can be read only.( I use python2 and xlrd,xlwt) – g.gong May 14 '17 at 10:40
-
You want to make ony some columns read only, or the whole worksheet? – redacted May 14 '17 at 11:22
-
I want to make ony some columns read only, but not the whole worksheet. – g.gong May 14 '17 at 14:14
-
I would be curious about the solution as well. Would you mind posting it as an answer? – redacted May 15 '17 at 13:25
0
this answer is come from: 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)
0
Thanks to Robin Nemeth for his answer. I made a little modification to it and got the required output.
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()
Here, data.to_excel() should be called before closing the connection, else will result in the following error "DatabaseError: DPI-1040: LOB was already closed".
Even removed the data.head() function, which didn't affect my output.

Anirudh
- 11
- 2
0
import cx_Oracle
import pandas as pd
import xlsxwriter
con = cx_Oracle.connect('user/password@IP:port/service_name')
with pd.ExcelWriter("", engine="xlsxwriter", options = {'strings_to_numbers': True, 'strings_to_formulas': False}) as writer:
try:
df = pd.read_sql("SELECT * FROM myTABLE", con)
df.to_excel(writer, sheet_name = "Sheet1", header = True, index = False)
print("File saved successfully!")
row = len(df.axes[0])
except:
print("There is an error")
-
1As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 21 '22 at 22:17