13 Working with Excel Spreadsheets Flashcards

1
Q

How do you

  1. Open an excel worksheet
  2. Get any sheet from the workbook?
  3. Get the active sheet?
  4. Get any and all sheetnames
A

How do you

  1. Open an excel worksheet
    »>import openpyxl
    »> wb = openpyxl.load_workbook(‘file.xlsx’)
  2. Get any sheet from the workbook?
    »>sheet = wb[‘sheetName’]
  3. Get the active sheet?
    »>sheet = wb.active
  4. Get any and all sheetnames
    »>sheet.title
    »>wb.sheetnames
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can you create a cell object?

Name 4x attributes that can be used on the cell object

A
  1. Choose sheet
    »> sheet = wb[‘Sheet1’]
  2. Create cell object
    »>co = sheet[‘A1’]

Useful methods:

co. value
co. row
co. column
co. coordinates->A1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Give a rundown of all the functions, methods and data types involved in reading a cell out of a spreadsheet file (1-8)

A
  1. > > > import openpyxl
  2. Create workbook object by calling the open function
    »>wb = openpyxl.load_workbook()
  3. Create a worksheet objectt with the active sheet or sheetnames attributes
    »>sheet = wb[‘sheetName’] OR
    »>sheet = wb.active
  4. Create a cell object using indexing or the cell() sheet method with row and column keyword
    c = sheet[‘A1’] / sheet[‘A1’:’B3’] OR
    c = sheet.cell(row1, column=2)
  5. Read the Cell object’s value attribute.
    c. value
    c. coordinate
    c. row / column
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can you create and save an excel File?

A

> > > import openpyxl
wb = openpyxl.load_workbook(‘example.xlsx’)
sheet = wb.active
sheet.title = ‘Spam Spam Spam’
wb.save(‘example_copy.xlsx’) # Save the workbook.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can you write to a cell and remove or create sheets??

A

Write to Cell: (Similar to writing to a dictionary)
»>sheet[‘A1] = ‘Text’

Create Sheet:
»>wb.create_sheet(index=0, title=’First Sheet’) OR
»> wb.create_sheet() # Default: sheetX and always last sheet in wb

Del Sheet:
del wb[‘sheetName’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can you find out the file size? (max columns and rows)

A

> > > wb = openpyxl.load_workbook(‘example.xlsx’)
sheet = wb[‘Sheet1’]
sheet.max_row
sheet.max_column

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you change the font? (Explain the structure)

A

> > > from openpyxl.styles import Font

Create a Font!
fontVariable = Font(kwargs=Desctiption)

Possible kwargs:

  • name (string)
  • size (integer)
  • bold (boolean)
  • italic (boolean)

E.g.
&raquo_space;> sheet = wb[‘Sheet’]
»> italic24Font = Font(size=24, italic=True) # Create a font.
»> sheet[‘A1’].font = italic24Font

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you include formulas, when writing to cells?

A

> > > sheet = wb.active
sheet[‘A3’] = ‘=SUM(A1:A2)’
wb.save(‘writeFormula.xlsx’)

The formula itself is as it would be written in Excel

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What value type do row/column_dimensions resemble?

How can you use them to change row/ column dimensions?

A

They resemble dictionary like values
row[i] -> integer
column[‘x’] -> letter

sheet. row_dimensions[1].height = 70
sheet. column_dimensions[‘B’].width = 20

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you merge cells? How can you undo merged cells?

A

> > > sheet.merge_cells(‘A1:D3’)

> > > sheet.unmerge_cells(‘A1:D3’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Explain Freezing Panes

How do you unfreeze?

A

All cells above and left of the defines cell are frozen, e.g. to freeze row 1 and Column A:
»>sheet.freeze_panes = ‘B2’

unfreeze:
»>sheet.freeze_panes = ‘A1’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Is it possible to create charts via Python?

A

Yes - but its so complicated, one should always look up how to do it!

> > > import openpyxl
wb = openpyxl.Workbook()
sheet = wb.active
for i in range(1, 11): # create some data in column A
… sheet[‘A’ + str(i)] = i

refObj = openpyxl.chart.Reference(sheet, min_col=1, min_row=1, max_col=1,
max_row=10)
seriesObj = openpyxl.chart.Series(refObj, title=’First series’)

> > > chartObj = openpyxl.chart.BarChart()
chartObj.title = ‘My Chart’
chartObj.append(seriesObj)

> > > sheet.add_chart(chartObj, ‘C5’)
wb.save(‘sampleChart.xlsx’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can you change a whole column or row, e.g. to switch the font?

A

> > > for row in range(1, N+1):
curCellCol = sheet.cell(row=row+1, column=1)
curCellCol.font = fontObj

How well did you know this?
1
Not at all
2
3
4
5
Perfectly