Practice 7 Slides
Practice 7 Slides
Practice 7 Slides
Practice session 7
Using modules and libraries in Python
SEDIN
IT Education Services Center
2
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
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
Formatting a cell:
MyActiveSheet['A1'].fill = openpyxl.styles.PatternFill (fill_type = 'solid', start_color = openpyxl.styles.colors.BLUE)
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:
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
Book references
Learning Python:
Chapter 11
Documentation of the modules (see url on each slide)
Assignment:
Exercises of lesson 15