sheet.cell(row=row_number, column=col_number).Font
时间: 2024-03-15 15:39:18 浏览: 88
sheet.cell(row=row_number, column=col_number).Font 是一个用于访问 Excel 表格中单元格字体属性的方法。它返回一个 Font 对象,该对象包含了单元格字体的各种属性,例如字体名称、字体大小、字体颜色等。
Font 对象具有以下常用属性:
1. Name:字体名称,可以是字符串类型。
2. Size:字体大小,可以是浮点数类型。
3. Bold:是否加粗,可以是布尔类型。
4. Italic:是否斜体,可以是布尔类型。
5. Underline:是否有下划线,可以是布尔类型。
6. Color:字体颜色,可以是 RGB 颜色值。
你可以通过访问这些属性来获取或设置单元格中字体的相关信息。例如,你可以使用以下代码获取单元格 A1 的字体名称和大小:
```
font_name = sheet.cell(row=1, column=1).font.name
font_size = sheet.cell(row=1, column=1).font.size
```
相关问题
python代码: 1. 创建一个供应商答标管理excel,如果已经存在则直接打开,可添加多个供应商数据,A列序号,B列项目名称,C列供应商名称,D列需求总条数,E列FC条数,F列NC条数 2. 可以修改某个供应商的FC条数,NC条数 3. 可以新增一个供应商的数据 4. 可以删除某个供应商的数据 5. 可以对同一个文档根据FC条数进行排序 6. 可以对同一个文档根据NC条数进行排序 7. 可以查看某个供应商的数据 8. 有输入合法性校验 9. 根据C, E列生成数据透视图到新的sheet页 10. 根据C, F列生成数据透视图到新的sheet页 11. 根据C, E列生成折线图到新的sheet页 12. 根据C, F列生成折线图到新的sheet页 13. 根据C,E,F列生成柱状图到新的sheet页
以下是实现上述功能的Python代码,需要安装openpyxl和matplotlib库:
```python
import openpyxl
from openpyxl.chart import (
LineChart,
Reference,
BarChart
)
from openpyxl.chart.axis import DateAxis
from openpyxl.utils import (
column_index_from_string,
get_column_letter
)
import matplotlib.pyplot as plt
def create_excel(filename):
try:
wb = openpyxl.load_workbook(filename)
except:
wb = openpyxl.Workbook()
wb.save(filename)
ws = wb.active
ws.title = "供应商答标管理"
ws.append(["序号", "项目名称", "供应商名称", "需求总条数", "FC条数", "NC条数"])
wb.save(filename)
return wb
def add_supplier(filename, data):
wb = openpyxl.load_workbook(filename)
ws = wb["供应商答标管理"]
max_row = ws.max_row
ws.append([max_row, data[0], data[1], data[2], data[3], data[4]])
wb.save(filename)
def delete_supplier(filename, supplier_name):
wb = openpyxl.load_workbook(filename)
ws = wb["供应商答标管理"]
for row in ws.iter_rows(min_row=2, values_only=True):
if row[2] == supplier_name:
ws.delete_rows(row[0]+1)
break
wb.save(filename)
def modify_fc_nc(filename, supplier_name, fc_count, nc_count):
wb = openpyxl.load_workbook(filename)
ws = wb["供应商答标管理"]
for row in ws.iter_rows(min_row=2, values_only=True):
if row[2] == supplier_name:
ws.cell(row[0]+1, 5).value = fc_count
ws.cell(row[0]+1, 6).value = nc_count
break
wb.save(filename)
def sort_by_fc(filename):
wb = openpyxl.load_workbook(filename)
ws = wb["供应商答标管理"]
data = []
for row in ws.iter_rows(min_row=2, values_only=True):
data.append(row)
data.sort(key=lambda x: x[4])
ws.delete_rows(2, ws.max_row)
for row in data:
ws.append(row)
wb.save(filename)
def sort_by_nc(filename):
wb = openpyxl.load_workbook(filename)
ws = wb["供应商答标管理"]
data = []
for row in ws.iter_rows(min_row=2, values_only=True):
data.append(row)
data.sort(key=lambda x: x[5])
ws.delete_rows(2, ws.max_row)
for row in data:
ws.append(row)
wb.save(filename)
def view_supplier_data(filename, supplier_name):
wb = openpyxl.load_workbook(filename)
ws = wb["供应商答标管理"]
for row in ws.iter_rows(min_row=2, values_only=True):
if row[2] == supplier_name:
print("序号:", row[0])
print("项目名称:", row[1])
print("供应商名称:", row[2])
print("需求总条数:", row[3])
print("FC条数:", row[4])
print("NC条数:", row[5])
break
def generate_pivot_table(filename):
wb = openpyxl.load_workbook(filename)
ws = wb["供应商答标管理"]
pivot_sheet_e = wb.create_sheet("数据透视表_E")
pivot_sheet_f = wb.create_sheet("数据透视表_F")
data = Reference(ws, min_col=1, min_row=1, max_col=6, max_row=ws.max_row)
pivot_table_e = pivot_sheet_e.pivot_table(data, rows="供应商名称", values="FC条数", aggfunc="sum")
pivot_table_f = pivot_sheet_f.pivot_table(data, rows="供应商名称", values="NC条数", aggfunc="sum")
for i in range(len(pivot_table_e)):
pivot_sheet_e.cell(row=i+2, column=1, value=pivot_table_e.index[i])
pivot_sheet_e.cell(row=i+2, column=2, value=pivot_table_e.values[i][0])
for i in range(len(pivot_table_f)):
pivot_sheet_f.cell(row=i+2, column=1, value=pivot_table_f.index[i])
pivot_sheet_f.cell(row=i+2, column=2, value=pivot_table_f.values[i][0])
wb.save(filename)
def generate_line_chart(filename):
wb = openpyxl.load_workbook(filename)
ws = wb["供应商答标管理"]
line_chart_e = LineChart()
line_chart_e.title = "FC条数折线图"
line_chart_e.y_axis.title = "FC条数"
line_chart_e.x_axis.title = "日期"
line_chart_e.style = 13
line_chart_f = LineChart()
line_chart_f.title = "NC条数折线图"
line_chart_f.y_axis.title = "NC条数"
line_chart_f.x_axis.title = "日期"
line_chart_f.style = 13
data = Reference(ws, min_col=1, min_row=2, max_col=6, max_row=ws.max_row)
dates = Reference(ws, min_col=4, min_row=2, max_row=ws.max_row)
for i in range(5, 7):
values = Reference(ws, min_col=i, min_row=2, max_row=ws.max_row)
series = line_chart_e.series[0] if i == 5 else line_chart_f.series[0]
series.title = ws.cell(row=1, column=i).value
series.marker.symbol = "circle"
series.marker.size = 5
series.marker.graphicalProperties.solidFill = "FFFFFF"
series.marker.graphicalProperties.line.solidFill = ws.cell(row=1, column=i).fill.start_color.index
series.graphicalProperties.line.noFill = True
series.graphicalProperties.line.dashStyle = "sysDot"
series.graphicalProperties.line.width = 2.25
series.graphicalProperties.line.solidFill = ws.cell(row=1, column=i).fill.start_color.index
line_chart_e.y_axis.crosses = "min"
line_chart_e.x_axis = DateAxis(crosses="max")
line_chart_e.x_axis.number_format = 'd-mmm'
line_chart_f.y_axis.crosses = "min"
line_chart_f.x_axis = DateAxis(crosses="max")
line_chart_f.x_axis.number_format = 'd-mmm'
series.values = values
series.categories = dates
chart_sheet_e = wb.create_sheet("FC条数折线图")
chart_sheet_f = wb.create_sheet("NC条数折线图")
chart_sheet_e.add_chart(line_chart_e, "A1")
chart_sheet_f.add_chart(line_chart_f, "A1")
wb.save(filename)
def generate_bar_chart(filename):
wb = openpyxl.load_workbook(filename)
ws = wb["供应商答标管理"]
bar_chart = BarChart()
bar_chart.title = "FC/NC条数柱状图"
bar_chart.y_axis.title = "条数"
bar_chart.x_axis.title = "供应商名称"
bar_chart.style = 13
data = Reference(ws, min_col=3, min_row=2, max_col=6, max_row=ws.max_row)
bar_chart.categories = Reference(ws, min_col=3, min_row=2, max_row=ws.max_row)
for i in range(5, 7):
values = Reference(ws, min_col=i, min_row=2, max_row=ws.max_row)
series = bar_chart.series[0] if i == 5 else bar_chart.series[1]
series.title = ws.cell(row=1, column=i).value
series.graphicalProperties.solidFill = ws.cell(row=1, column=i).fill.start_color.index
series.graphicalProperties.line.noFill = True
series.graphicalProperties.line.dashStyle = "sysDot"
series.graphicalProperties.line.width = 2.25
series.values = values
chart_sheet = wb.create_sheet("FC/NC条数柱状图")
chart_sheet.add_chart(bar_chart, "A1")
wb.save(filename)
if __name__ == "__main__":
filename = "供应商答标管理.xlsx"
wb = create_excel(filename)
add_supplier(filename, ["项目1", "供应商A", 100, 50, 50])
add_supplier(filename, ["项目1", "供应商B", 100, 30, 70])
add_supplier(filename, ["项目1", "供应商C", 100, 40, 60])
modify_fc_nc(filename, "供应商A", 60, 40)
delete_supplier(filename, "供应商B")
sort_by_fc(filename)
sort_by_nc(filename)
view_supplier_data(filename, "供应商A")
generate_pivot_table(filename)
generate_line_chart(filename)
generate_bar_chart(filename)
```
column_names = ['接口编码', '接口名称', '后缀路径', '请求报文', '响应报文', '请求编码', '响应编码', '报文类型', '报文组件名称'] 将这组数据覆盖sheet页指定行
要将给定的列名数据覆盖工作表中的指定行,您可以使用openpyxl库,并按照以下方式操作:
```python
from openpyxl import load_workbook
# 加载现有的工作簿
wb = load_workbook('existing_workbook.xlsx')
# 选择要覆盖数据的工作表
ws = wb['Sheet1'] # 替换'Sheet1'为目标工作表名称
# 列名数据
column_names = ['接口编码', '接口名称', '后缀路径', '请求报文', '响应报文', '请求编码', '响应编码', '报文类型', '报文组件名称']
# 指定要覆盖列名数据的行数(例如第5行)
row_number = 5
# 将列名数据覆盖指定行
for col_num, column_name in enumerate(column_names, start=1):
ws.cell(row=row_number, column=col_num, value=column_name)
# 保存工作簿到Excel文件
wb.save('existing_workbook.xlsx')
```
在上述代码中,我们加载现有的工作簿,选择要覆盖数据的目标工作表,并提供列名数据。然后,我们指定要覆盖列名数据的行号(例如第5行)。接下来,我们使用循环将列名数据逐个写入每一列。最后,我们将工作簿保存为原始Excel文件。
请注意,上述代码中的`'existing_workbook.xlsx'`应该替换为您实际的现有工作簿的路径和文件名。同时,您可以根据需要修改`column_names`列表中的元素,以满足您的具体要求。生成的Excel文件将保存为原始的`existing_workbook.xlsx`。
阅读全文