foreword
Students who have used Pandas and openpyxl libraries know that these two libraries are complementary to each other. Pandas is definitely the fastest and best library for working with Excel in Python, but some of the advantages of using openpyxl are the ease of customizing spreadsheets with styles, conditional formatting, etc.
If you want to easily use Pandas to process Excel data, but also want to add some styles to Excel spreadsheets, what should you do?
But guess what, you don't have to worry about picking.
In fact, openpyxl supports converting data from a Pandas DataFrame to a workbook, or conversely, converting an openpyxl workbook to a Pandas DataFrame.
DataFrame to Workbook
Let's create a DataFrame first:
import pandas as pd
data = {
"姓名": ["张三", "李四"],
"性别": ["男", "女"],
"年龄": [15, 25],
}
df = pd.DataFrame(data)
df
The result is as follows:
If you want to set the header to be red and centered, how should you set it?
from openpyxl import Workbook
from openpyxl.utils.dataframe import dataframe_to_rows
from openpyxl.styles import Font
from openpyxl.styles import Alignment
wb = Workbook()
ws = wb.active
for row in dataframe_to_rows(df, index=False, header=True):
ws.append(row)
font = Font(name="微软雅黑",size=10, bold=True,italic=False,color="FF0000")
alignment = Alignment(horizontal="center",vertical="center")
for i in range(1,df.shape[1]+1):
cell = ws.cell(row=1, column=i)
print(cell.value)
cell.font = font
cell.alignment = alignment
wb.save("pandas.xlsx")
The result is as follows:
Workbook to DataFrame
If we have such a piece of data and we want to convert it to a DataFrame, what should we do?
In fact, this is a bit superfluous. After we read it directly with pandas, after processing the data, can't we just design the style? Why do you have to use openpyxl to read the workbook in the first place?
Haha, but since this method is provided in openpyxl, let's take a look.
import pandas as pd
from openpyxl import load_workbook
workbook = load_workbook(filename="df_to_openpyxl.xlsx")
sheet = workbook.active
values = sheet.values
df = pd.DataFrame(values)
df
The result is as follows: