Ad

How To Read Configurations Parameters From An INI File?

- 1 answer

I want to set some paths of excel files in the INI file to be read by my code in Python. Can you please tell me how to call them? In my ini file I have something like this:

[common]

default_path = "C:/Users/XX/Desktop/Bk.xlsx/"
Ad

Answer

  1. For loading excel file install xlrd from pypi.

Command to install xlrd module : pip install xlrd

In config.ini dont need " so:

[general]
default_path = C:/Users/XX/Desktop/Bk.xlsx/

sample code:

import xlrd
import configparser

#Loading config
config = configparser.ConfigParser()
config.read("config.ini")
Excel_PATH = config["general"]["default_path"]

# Reading an excel file using Python 
wb = xlrd.open_workbook(Excel_PATH) 
sheet = wb.sheet_by_index(0) 

# For row 0 and column 0 
sheet.cell_value(0, 0) 
Ad
source: stackoverflow.com
Ad