PYTHON FOR ABSOLUTE BEGINNERS – PART5

PACKAGES

To define a package, needs to create folder, and then inside it create a file as name “__init__.py”

51

Consider that in to_pay.py we have a simple code like:

def list_of_payments():
     print("payment")

Now in app.py, we can import this package.

import bills.to_pay
bills.to_pay.list_of_payments()

there is another way:

from bills.to_pay import list_of_payments,...
list_of_payments()

or another way:

from bills import to_pay,...
to_pay.list_of_payments()

here you can find a full list of internal python modules

here you can find a library of packages PYPI
to know how to find and install packages, for example, search for “openpyxl”, and then select openpyxl 2.6.2
in top of the page, you’ll find in command like “pip install openpyxl
you should type this command in terminal, to install the package.

If you got error that pip is not recognized by your system, then should install it.so, save get-pip.py some where in your computer, and in terminal or command window, type python get-pip.py. Now pip command works!
But if you got python command is not recognized by your system, then may be should go back to installation. so go to your python installation package that already downloaded and installed on your system and select  modify.
52
then to next step and find “Add Python to environment variables” and if it’s not checked, so select it and then install.
53
now close and open the command window again, and should work!

Example:

imagine that we have a excel file called a.xlsx like below

54

now we want to read the information of Sheet1 and create a chart

import openpyxl as xl
import openpyxl.chart as xlch
wb = xl.load_workbook('python/a.xlsx')
sheet = wb['Sheet1']
values = xlch.Reference(sheet, min_row=2, max_row=sheet.max_row,

                        min_col=2, max_col=4)

chart = xlch.BarChart()
chart.add_data(values)
sheet.add_chart(chart)
wb.save('python/a.xlsx')

the result will be this:

55

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s