Detailed explanation of python's operation of csv and excel files

Table of contents

1. csv file

2. Excel table

2.1 Read excel file

2.2 Write to excel file

2.3 Cell font style

2.4 Official

2.5 Adjust rows and columns

2.6 Merge and split cells

2.7 Freeze window

2.8 Icon table


1. csv file

csv is also called a comma-separated value file - the content of each line is distinguished by commas.

csv files can be opened directly through excel. Save the display file in rows and columns. Compared with Excel, it can only save data, but not formulas and functions.

csv.reader (file object) - Get the file content and return the content of each line in a list

The following example is a write operation to a CSV file:

with open('data.csv','w',) as csvfile:
    writer = csv.writer(csvfile,delimiter = ' ')
    writer.writerows([['id','name','age'],['10001','Mike',20],['10002','Bob',22],['10003','Jordan',21]])
    writer.writerow(['10004','Long',25])
​
with open('data.csv','w',) as csvfile:
    fieldnames = ['id','name','age']
    writer = csv.DictWriter(csvfile,fieldnames=fieldnames,delimiter='\t')
    writer.writeheader()
    writer.writerow({'id':'10001','name':'Mike','age':21})
    writer.writerow({'id':'10002','name':'Bob','age':22})
    writer.writerow({'id':'10003','name':'Jordan','age':25})
​
with open('data.csv','a',encoding='utf-8') as csvfile:
    fieldnames = ['id','name','age']
    writer = csv.DictWriter(csvfile,fieldnames=fieldnames,delimiter='\t')
    writer.writeheader()
    writer.writerow({'id':'10010','name':'小子','age':30})

The wirterow method can write a single row, and the delimiter parameter sets the gap between each column. The witerrows method can write multiple rows. At the same time, if you want to write in the form of a dictionary, you can use the DictWriter class to generate an object and then pass in the dictionary.

To read csv, you can use the csv module or the pandas module. The example is as follows:

with open('data.csv','r',encoding='utf-8') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        print(row)
#ctrl + b查看内置底层文件代码
import pandas as pd
​
df = pd.read_csv('data.csv')
for index, row in df.iterrows():
    print(row.tolist()) #以列表的形式输出

2. Excel table

2.1 Read excel file

Using the openpyxl.load_workbook method of openpyxl, you can open an excel file. This method accepts a file name and returns a value of the workbook data type. The created object represents the excel file:

import openpyxl
​
workbook = openpyxl.load_workbook('csv.xlsx') #打开一个xlsx文件
print(type(workbook))
得到一个excel文件对象之后,访问sheetnames属性可以取得工作簿所有表名的列表,示例:

print(workbook.sheetnames) #得到所有工作表名,返回一个列表
sheet = workbook.sheetnames
print(sheet)

Activity sheets are also available, examples are as follows:

print(workbook.sheetnames) #得到所有工作表名,返回一个列表
sheet = workbook.sheetnames
# print(sheet)
sheet1 = workbook.active #获取活动表
print(sheet1)
print(sheet1.title)  #获取活动表,即选中的表的名字

After getting the sheet object, you can access the Cell object by name. The value of the Cell object contains the value of the cell. The row attribute is the number of rows of the cell. Column is the number of columns of the cell. Both rows and columns start from 1. . The coordinate attribute is the name of the cell, such as A1

Examples are as follows:

#访问Cell对象
print(sheet1['D5'])
D5 = sheet1['D5']
print(D5.value)
print(D5.row)
print(D5.column)
print(D5.coordinate)
​
输出:
<Cell 'Sheet1'.D5>
0.85242
5
4
D5

If you want to output multiple data, you can use the sheet.cell() method, passing in the row parameters and column parameters, and you can also get a Cell object. The example is as follows:

for i in range(1,10):
    for j in range(1,5):
        print(i,j,":",sheet1.cell(row=i, column=j).value)

If you want to output all data, you can determine the size of the table through the max_row and max_column attributes. The example is as follows:

print(sheet1.max_row,sheet1.max_column)
# for i in range(1,sheet1.max_row + 1):
#     for j in range(1,sheet1.max_column + 1):
#         print(i,j,":",sheet1.cell(row=i, column=j).value)

If you want to convert column numbers into letters, you can call the openpyxl.utils.get_column_letter() function. If you want to convert letters into numbers, you can call the openpyxl.utils.column_index_from_string() function.

print(openpyxl.utils.get_column_letter(sheet1.max_column))
print(openpyxl.utils.column_index_from_string('AA'))

The worksheet object supports slicing, and you can obtain all Cell objects in a row, a column, or a rectangular area in the spreadsheet. Then iterate through all cells in this cell:

print(list(sheet1['A1':'D5']))
for rowofCellObjects in sheet1['A1':'D5']:
    for cellObject in rowofCellObjects:  #得到每一行的单元格对象
        print(cellObject.coordinate, cellObject.value) #每个单元格对象

If you want to access the value of a cell in a specific row or column, you can use the rows and columns properties of the Worksheet object. These properties must be converted into a list by the list() function before square brackets and indexes can be used. Examples are as follows:

#访问第二列的值
print(list(sheet1.columns)[2])
for cellObj in list(sheet1.columns)[2]:
    print(cellObj.coordinate, cellObj.value)
​
#访问第二行的值
print(list(sheet1.rows)[2])
for cellObj in list(sheet1.rows)[2]:
    print(cellObj.coordinate, cellObj.value)

2.2 Write to excel file

Create and save an excel document and call the openpyxl.Workbook() function to create a new empty Workbook object. The example is as follows:

import openpyxl
import os
#创建excel文件对象
workbook = openpyxl.Workbook()  #新建工作薄
print(workbook.sheetnames)
os.path.exists('./data2.xlsx') or workbook.save('data2.xlsx')  #不存在则创建

Create and delete worksheets. Use the create_sheet() method to create a worksheet. The index parameter specifies the location of the worksheet, and the title parameter specifies the name of the worksheet.

#创建新的工作表
workbook.create_sheet(index=1, title='sheet2')
print(workbook.sheetnames)
​
del workbook['sheet2']
print(workbook.sheetnames)
workbook.save('data2.xlsx') 

Write data, like a dictionary, using the string of cell coordinates as the key value, used in the Worksheet object, specifying the cell to be written:

sheet1 = workbook.active
sheet1['A1'] = 'hello world'
workbook.save('data2.xlsx')

You can also assign a value to the cell object:

sheet1 = workbook.active
sheet1['A1'] = 'hello world'
sheet1.cell(row=1,column=2).value = 'hello world'
sheet1.cell(row=1,column=5).value = 55
workbook.save('data2.xlsx')

Note that the data2.xlsx file cannot be opened when using the save method.

2.3 Cell font style

In order to define the font style of the cell, you need to import the Font() function from the openpyxl.styles module

The process of setting font is as follows:

import openpyxl
from openpyxl.styles import Font
​
wrokbook = openpyxl.Workbook()
​
sheet = wrokbook.active
italic24Font = Font(size=24, italic=False) #创造一种字体风格  字号24,斜体
sheet['A1'].font = italic24Font #将这种风格用于单元格A1
sheet['A1'] = 'Hello world!'
wrokbook.save('data2.xlsx')

Keyword arguments to the Font style attribute

name: String font name, such as "Calibri" or "Times New Romam"

size: plastic font size

bold: Boolean type whether to be bold or not

italic: whether the Boolean type is italic

Example:

import openpyxl
from openpyxl.styles import Font
​
wrokbook = openpyxl.Workbook()
​
sheet = wrokbook.active
italic24Font = Font(name= 'Times New Roman',bold=True,size=24, italic=True) #创造一种字体风格  字号24,斜体
sheet['A1'].font = italic24Font #将这种风格用于单元格A1
sheet['A1'] = 'Hello world!'
wrokbook.save('data2.xlsx')

2.4 Official

Formulas in Excel begin with an equal sign, and cells can be configured to contain values ​​calculated from other cells. In python, the operation method is the same as writing data into excel. Use a certain cell as the key value and enter the corresponding formula.

wrokbook = openpyxl.load_workbook('data.xlsx')
sheet = wookbook.active
​
sheet['D25'] = '=SUM(I2:I23)'
print(sheet['D25'].value)
wrokbook.save('data.xlsx') #保存更改

The written data can be seen in the excel table.

2.5 Adjust rows and columns

The Worksheet object has row_dimensions and column_dimensions attributes, which are used to control row height and column width respectively:

wrokbook = openpyxl.Workbook()
​
sheet = wrokbook.active
sheet['A1'] = 'tall row'
sheet['B2'] = 'wide column'
sheet.row_dimensions[1].height = 70  #设置列高
sheet.column_dimensions['B'].width = 20  #设置列宽
wrokbook.save('test.xlsx')

2.6 Merge and split cells

Using the merge_cells() working method, cells in a rectangular area can be merged into one cell. The example is as follows:

wrokbook = openpyxl.Workbook()
​
sheet = wrokbook.active
​
sheet.merge_cells('A1:D3') #合并表格
sheet['A1'] = 'Twelve cells merged together'
sheet.merge_cells('C5:D5') #
sheet['C5'] = 'Two merged cells'
wrokbook.save('data2.xlsx')

To split cells, use the unmerge_cells method. The example is as follows:

sheet.unmerge_cells('A1:D3')
sheet.unmerge_cells('C5:D5')
wrokbook.save('data2.xlsx')

2.7 Freeze window

For spreadsheets that are too large to fit on one screen, it is very helpful to "freeze" the top rows or the leftmost columns. After freezing, the frozen column or row headers will always be visible even if the user scrolls the spreadsheet. Yes, this is called "freezing panes".

In openpyxl, each Worksheet object has a freeze_panes attribute, which can be set to a Cell object or a string of cell coordinates. It should be noted that all rows above the cell and all columns to the left will be frozen. However, the row and column where the cell is located will not be frozen.

To freeze all cells, set freeze_panes to None or 'A1'.

Example to freeze panes:

freeze_panes sets frozen rows and columns

sheet.freeze_panes = 'A2' 行1

sheet.freeze_panes = 'B1' 列A

sheet.freeze_panes = 'C1' Column A and Column B

sheet.freeze_panes = 'C2' Row 1, Column A and Column B

sheet.freeze_panes = 'A1' has no frozen panes

sheet.freeze_panes = 'None' No frozen panes

wrokbook = openpyxl.load_workbook('data2.xlsx')
sheet = wrokbook.active
​
sheet.freeze_panes = 'A2' #冻结第一行
wrokbook.save('data2.xlsx')

2.8 Icon table

openpyxl supports using data from cells in the worksheet to create bar charts, line charts, scatter charts, and pie charts. There are following steps to create a chart:

1. Select cells from a rectangular area to create a Reference object.

2. Create a Series object by passing in the Reference object

3. Create a Chart object.

4. Add the Series object to the Chart object.

5. Optionally set the drawing.top, drawing.left, drawing.width and drawing.height properties of the Chart object.

6. Add the Chart object to the Worksheet object.

Reference objects require some explanation. The Reference object is obtained by calling openpyxl.charts. The Reference() function is created by passing in the following three parameters.

1. Worksheet object containing chart data.

2. A tuple of two integers, representing the upper-left corner cell of the rectangular selection area, which contains chart data: the bottom integer in the tuple is the row, and the second integer is the column. What needs to be noted here is that the first line is 1 instead of 0.

3. A tuple of two integers, representing the lower-right cell of the rectangular selection area, which contains chart data: the first integer in the tuple is the row, and the second integer is the column.


workbook = openpyxl.load_workbook('data2.xlsx')
​
sheet = workbook.active
for i in range(1,11):
    sheet['A' + str(i)] = i #给单元格赋值
​
#选择X、Y轴数据,并创建成一个数据系列,这是绘画散点图的步骤
xdata=openpyxl.chart.Reference(sheet, min_col = 1, min_row = 1, max_col = 1, max_row = 10)
ydata=openpyxl.chart.Reference(sheet, min_col = 1, min_row = 1, max_col = 1, max_row = 10)
series1=openpyxl.chart.Series(ydata,xdata,title_from_data=False)
​
series1.marker.symbol = "circle"
# 设置系列1数据点的颜色,以下两行代码将其改为红色
series1.marker.graphicalProperties.solidFill = "FF0000"  # 点的内部填充颜色
series1.marker.graphicalProperties.line.solidFill = "FF0000"  # 点的外边框颜色
​
# 关键的一步:关闭系列1数据点之间的连接线
series1.graphicalProperties.line.noFill = True
​
​
​
refobj = openpyxl.chart.Reference(sheet, min_col = 1, min_row = 1, max_col = 1, max_row = 10) # 创建Reference对象
seriesobj = openpyxl.chart.Series(refobj, title = 'First series') #传入reference对象创建一个series对象
​
chartobj = openpyxl.chart.BarChart()  #创建chart对象
chartobj.title = '条形图'  #图标标题
#设置x轴和y轴
chartobj.y_axis.title='Y轴'
chartobj.x_axis.title='X轴'
chartobj.append(seriesobj)  #将series添加到chart对象
​
#画一个条形图
sheet.add_chart(chartobj, 'C5')  #左顶角位于C5
workbook.save('data2.xlsx')
​
#画一个折线图
chartobj = openpyxl.chart.LineChart()  #创建chart对象
chartobj.title = '折线图'  #图标标题
#设置x轴和y轴
chartobj.y_axis.title='Y轴'
chartobj.x_axis.title='X轴'
chartobj.append(seriesobj)  #将series添加到chart对象
​
​
sheet.add_chart(chartobj, 'C20')  #左顶角位于C5
workbook.save('data2.xlsx')
​
# #画一个散点图
chartobj = openpyxl.chart.ScatterChart()  #创建chart对象
chartobj.title = '散点图'  #图标标题
#设置x轴和y轴
chartobj.y_axis.title='Y轴'
chartobj.x_axis.title='X轴'
chartobj.append(series1)  #将series添加到chart对象
​
​
sheet.add_chart(chartobj, 'C35')  #左顶角位于C5
workbook.save('data2.xlsx')
​
#画一个饼图
chartobj = openpyxl.chart.PieChart()  #创建chart对象
chartobj.title = '饼图'  #图标标题
chartobj.append(seriesobj)  #将series添加到chart对象
​
​
sheet.add_chart(chartobj, 'C55')  #左顶角位于C5
workbook.save('data2.xlsx')

The above code uses 4 methods:

The BarChart() method draws a bar chart.

LineChart() method draws a line chart.

The ScatterChart() method draws a scatter chart.

The PieChart() method draws a pie chart.

Guess you like

Origin blog.csdn.net/longhaierwd/article/details/131971722