Sunteți pe pagina 1din 12

COMPUTER SCIENCE

code 30424 a.y. 2018-19

Practice session 7
Using modules and libraries in Python

SEDIN
IT Education Services Center
2

Objectives of the practice session


• Deepen the practical use of modules in Python
• Get to know the essential objects of some of the most common modules
• Develop simple scripts making use of modules
3

Modules used in this practice session


Python Standard Library
• os
• webbrowser

Python Package Index collection


• openpyxl
4

What to do before starting the exercise


• Check if you have installed all the required modules:

• Install the modules you need:


5

os module
Standard module that provides a set of tools to make possible the use in
Python of operating system dependent functionalities.
Some useful functions, in addition to those already examined:

os.getlogin ()
Return the name of the current Windows user

os.getcwd()
Return the path of the current working directory

os.chdir (path)
Change the current working directory to path.
Note: the argument path should be written as a text string, e.g. 'G:\\Python\\Sample Folder'
https://docs.python.org/3/library/os.html
6

openpyxl module
Non standard Python module for reading and writing Excel files.
Some useful functions, in addition to those already examined:

MyNewXLfile = openpyxl.Workbook ()
Creates the Excel file MyNewXLfile in memory (not yet on a disk drive). A new empty
workbook has always one single worksheet

MyNewXLfile.save ('MyFile.xlsx')
Save the workbook MyNewXLfile as MyFile.xlsx on the active path

MyNewXLfile.active.title = 'MySheet'
Rename the active worksheet in MyNewXLfile workbook as MySheet
https://openpyxl.readthedocs.io/en/stable/
7

More openpyxl functions


MyNewXLfile.create_sheet ('MyNewSheet')
Creates a new worksheet in MyNewXLfile workbook, inserting it by default at the end. Using
('MyNewSheet', n) as arguments, the worksheet is inserted at position n (where n is a number,
starting from 0 as first position).

MyNewXLfile.sheetnames
Returns the names of all worksheets in MyNewXLfile workbook.
Note: the type returned is a List.

openpyxl.load_workbook ('MyFile.xlsx')
Load in memory the existing file MyFile.xlsx.

https://openpyxl.readthedocs.io/en/stable/
8

Still more openpyxl functions


MyNewXLfile['MySheet']
Activate the worksheet MySheet in MyNewXLfile workbook

MyNewXLfile['MySheet']['A1'] = 'Hi guys!'


Writes 'Hi guys!' in cell A1 of MySheet worksheet of MyNewXLfile workbook

Formatting a cell:
MyActiveSheet['A1'].fill = openpyxl.styles.PatternFill (fill_type = 'solid', start_color = openpyxl.styles.colors.BLUE)

MyActiveSheet['A1'].font = openpyxl.styles.Font (italic = True, bold = True, color = openpyxl.styles.colors.WHITE)

MyActiveSheet['A1'].alignment = openpyxl.styles.Alignment (horizontal = 'center')

https://openpyxl.readthedocs.io/en/stable/
9

webbrowser module
It is a Web-browser controller that provides a high-level interface to allow
displaying Web-based documents to users.
Some useful functions, in addition to those already examined:

webbrowser.open (url, new=0)


Display the web page at url using the default browser. If new is 0, the url is
opened in the same browser window, if possible. If new is 1, a new browser
window is opened, if possible. If new is 2, a new browser page (“tab”) is
opened, if possible

https://docs.python.org/3.6/library/webbrowser.html
10

Exercises
1. Creating on a local drive a new Excel file to manage the score
of first semester exams

2. Searching on Wikipedia the page of a town chosen by the user


11

Files of the lesson


In the zip file 30424 ENG - 31 - Practice 7.zip you will find these files:
• 30424 Practice 7 slides.pdf: these slides
• Practice 7 30424 ENG.pdf: text of the exercises
• Solved files .py
12

Book references
Learning Python:
Chapter 11
Documentation of the modules (see url on each slide)

Assignment:
Exercises of lesson 15

S-ar putea să vă placă și