music21

The Python library music21 is comprehensively documented on the project page of MIT Boston: In addition to a User Guide with chapters on numerous topics, there is an extensive module documentation and an alphabetical index concering all topics and commands.
Especially in view of this very extensive documentation, it makes sense to list some important commands related to computer-aided analysis procedures separately and to describe them with short examples.

Overview of the most important music21 commands

In the following, the most important and useful commands in music21 are listed. More complex command sequences can be found with examples in the tutorials notes.

(1) Handling files

The following commands load the music21 library and then the Prelude in C major from the Well-Tempered Clavier as variable 'sBach'.

 from music21 import *
 # This initial command imports the Python library music21
 
 sBach = converter.parse('https://analyse.hfm-weimar.de/database/04/BaJoSe_BWV846_COM_COM_DasWohltem_004_00816.xml')
 

Now the piece will be displayed with MuseSore:

 sBach.show()			

Here's how to see the score information in musicxml format:

 sBach.show('text') 

And this is how the score is played as midi:

 sBach.show('midi') 			

Selection of bars / parts:

 a = sBach.parts[0] 		
 # selects the first voice (here: right hand) as variable 'a'. 
 b = sBach.measures(4,7)		
 # chooses measure 4 to 7 as variable 'b'
 c = sBach.parts[0].measures(2,4)	
 # chooses bar 2 to 4 of the first voice as 'c'
 Alternative: sBach.measures(2,4).show()
 # selects and displays bar 2 to 4.

Show number of elements:

 len(sBach)	
 # shows the number of elements on the next lower level
 len(sBach.flat)	
 # names the number of all contained elements, e.g. notes

Save the selection:

 sBach.write()
 a.write()	
 # saves the variables 'sBach' or 'a' (see above: the first voice) as xml-file (default)
 # Other formats can be chosen, e.g. 
 sBach.write('midi')
 # alternative command for saving the first voice:
 sBach.parts[0].write()

(2) View graphs:

 sBach.plot('pianoroll')		
 sBach.parts[2].plot('pianoroll')		
 # only the second voice is displayed as pianoroll
 sBach.measure(2,5).plot('pianoroll')	
 # only measures 2-5 are displayed
 graph.plotStream(sBach) 			
 # = alternative method

 sBach.plot('histogram')			
 # histogram of pitch frequencies

(3) Writing notes

 n1 = note.Note('g4', type='quarter')
 n2 = note.Note('a4', type='quarter')
 n3 = note.Note('g4', type='half')
 # notes are connected ("streamed") to measures, measures to parts and parts to scores:
 m1 = stream.Measure()
 m1.append([n1, n2, n3])
 p1 = stream.Part
 p1.append([m1])