import json
import random
import sys,requests
import openpyxl
from openpyxl.styles import Font, PatternFill
import numpy as np
import pandas as pd
from pandas.io.json import json_normalize
from matplotlib import pyplot as plt
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QGroupBox, QHBoxLayout, QMenu, QDialog, \
QLabel
class MyWin(QWidget):
def __init__(self):
super().__init__()
self.setUI()
def setUI(self):
self.setWindowTitle('Office_Study')
mainLay = QVBoxLayout()
self.setLayout(mainLay)
# 第1章
Part1_Button = QPushButton('第1章 Excel')
Part1_Menu = QMenu()
Part1_Button.setMenu(Part1_Menu)
mainLay.addWidget(Part1_Button)
P1_1=Part1_Menu.addAction('01.工作薄对象')
P1_1.triggered.connect(self.P1_1)
P1_2 = Part1_Menu.addAction('02.sheet对象')
P1_2.triggered.connect(self.P1_2)
P1_3 = Part1_Menu.addAction('03.cell对象')
P1_3.triggered.connect(self.P1_3)
P1_4 = Part1_Menu.addAction('04.读取单元格属性')
P1_4.triggered.connect(self.P1_4)
def P1_1(self):
# 一、创建xlsx对象的两种方法
# (1)读取xlsx文件 openpyxl.load_workbook()
open_xls=openpyxl.load_workbook()
# (2)创建xlsx文件 openpyxl.Workbook()
creat_xls=openpyxl.Workbook()
# 二、xlsx的方法
# (1)对象.save() 保存xlsx文件
creat_xls.save()
# (2)对象.create_sheet() 创建工作表
creat_xls.create_sheet()
# (3)对象.add_image() 插入图片(在某个单元格中)
image = openpyxl.drawing.image.Image('./Excel/狼.jpeg') # 创建图片对象
image.height=60
image.width=50
creat_xls.add_image(image,"F6") # 在工作表中插入图片
# 四、通用方法
# (1)删除工作表 def 工作名对象
# 三、xlsx的属性
# (1)对象.sheetnames xlsx文件中所有工作表的名称
creat_xls.sheetnames
# (2)对象.active xlsx文件的当前工作表
creat_xls.active
def P1_2(self):
# 一、创建sheet对象
# (1)变量=工作薄对象['工作表名称']
open_xls=openpyxl.load_workbook('./Excel/工作表对象.xlsx')
sheet1=open_xls['2月']
print(sheet1)
# (2)变量=工作薄对象.active
sheet2=open_xls.active
print(sheet2)
# 二、sheet对象的方法
# (1)合并和拆分单元格
# sheet1.merge_cells('A1:D6') # 将A1: D6区域合并为一个单元格
# sheet2.unmerge_cells('c8:g9') # 将整体合并区域拆分成单元格
# 三、sheet对象的属性
# (1)创建cell对象
cell1=sheet1['B6']
cell2=sheet1.cell(row=6, column=3) # 创建单元格对象
# (2)创建区域对象
zrea1=sheet1['A1:D6']
# (3)获取工作表中的最大行和最大列的数量
n1=sheet1.max_row
n2=sheet1.max_column
# (4)设置行高和列宽
sheet1.row_dimensions[1].height=200
sheet1.column_dimensions['B'].width=100
'''
a.行的高度可以设置为0到409之间的整数或浮点值。这个值表示高度的点数。一点等于1/72英寸。默认的行高是12.75.
b.列宽可以设置为0到255之间的整数或浮点数。默认的列宽是8.43个字符。
c.列宽为零或行高为零,将使单元格隐藏。
'''
# (5)修改工作表的名称
sheet1.tilte='张三'
# (6)设置打印属性
sheet1.print_area='A1:F20' # 设置打印区域
sheet1.print_title_rows='1:1' # 设置打印标题行
sheet1.print_title_cols='B:C' # 设置打印标题列
'''
https://blog.csdn.net/debrnr/article/details/124067362
'''
open_xls.save('./Excel/工作表对象.xlsx')
def P1_3(self):
# 一、创建cell对象
open_xls = openpyxl.Workbook()
sheet1 = open_xls.active
# (1)方法一
cell1 = sheet1['B6']
# (2)方法二
cell2 = sheet1.cell(row=6,column=2)
# 二、cell对象的方法
# 三、cell对象的属性
# (1)cell对象..value 单元格的值
cell1.value='张三'
'''
单元格的值可以为数字、字符、公式
比如
cell1.value='张三' 值为字符
cell1.value=100 值为数字
cell1.value='=a1*b1' 值为公式,即在单元格中插入公式
'''
# (2)cell对象.row 单元格所在的行号
a1=cell1.row
print(a1)
# (3)cell对象.column 单元格所在的列号
a2=cell1.column
print(a2)
# (4)cell对象.coordinate 单元格所在的坐标
a3=cell1.coordinate
print(a3)
# (5)cell对象.font = 变量 设置单元格的字体
'''
先创建字体对象,再为单元格对象的font属性赋值即可。
'''
font=openpyxl.styles.Font(name='楷体',size='28',color='7F00FF')
cell1.font=font
'''
为了定义单元格的字体风格,需要从openpyxl.styles模块导入Font
字体对象
font=Font(name='Calibri',size=11,bold=False,italic=False,vertAlign=None,underline='none',strike=False, color='FF000000')
参数解读:
name:字体名称
字体名称,注意中文字体前面加u
size:设置字体大小
字号大小
bold:设置是否加粗
True(加粗)/False(不加粗)
italic:
True(倾斜)/False(不倾斜)
vertAlign:设置上下标
'None"(默认)/
'superscript'(上标)/
'subscript'(下标)
underline:设置下划线
'None',(fi)/
'single’(单下列线)/
'double’(双下划线)/
'singleAccounting’(会计用单下列线)/
'doubleAccounting’(会计用双下划线
strike:设置删除线
'True"(显示删除线)/
'False"(不显示删除线)
color:颜色
字体的颜色 RGB转HEX
RGB转HEX 指的一个RGB转HEX的色号转换网站,比如:
https://www.jyshare.com/front-end/55/
'''
# (6)cell对象.alignment = 变量 设置单元格对齐方式
'''
先创建对齐对象,再为单元格对象的alignment属性赋值即可。
'''
alignment=openpyxl.styles.Alignment(vertical='top', horizontal='right',text_rotation=45)
cell1.alignment=alignment
'''
# 1.创建对齐对象
alignment=Alignment(horizontal='general',vertical='bottom', text rotation=0, wrap text=False, shrink to fit=False, indent=0)
# 2.参数解读
horizontal:
'general'(常规)/
'justify'(两端对齐)/
'right'(靠右)/
'centercontinuous'(跨列居中)/
'distributed'(分散对齐)/
'fill'(填充)/
center'(居中)/
'left'(靠左)
vertical:
'center'(垂直居中)/
'top'(靠上)/
'bottom'(靠下)/
justify'(两端对齐)/
'distributed'(分散对齐)
text_rotation:指定文本旋转角度
wrap_text:是否自动换行
shrink_to_fit:是否缩小字体填充
indent:指定缩进
'''
# (7)cell对象.border 设置单元格的边框样式
'''
先创建side对象,再创建border对象,最后为cell.border属性赋值
'''
side=openpyxl.styles.Side(style='double',color='ff0000')
border=openpyxl.styles.Border(left=side,right=side,top=side,bottom=side)
cell1.border=border
'''
1.设置cel的边框样式
需要使用Side, Boder类,步骤:
a.创建Side对象,通过构造方法参数style和color设置其边的样式和颜色
b.设置cl的border属性,给其赋值为Border对象, 且设置其上下左右边框为哪-个Side
2.设置单元格边框线的方法
s1=Side(style='thin',color='8470FF')
style表示边框线类型,color表示边框线颜色
style参数的种类:
'double'
'mediumDashDotDot'
'dashed'
'dashDot'
'slantDashDot'
'dashDotDot'
'dotted'
"hair'
'mediumDashed',
'thin'
'mediumDashDot’
'medium'
'thick'
3.设置单元格边框的方法
sheet1['b5'].border=Border(top=s1,bottom=s2,left=s1,right=s2)
top,bottom,left,right分别传入边框线的类型,即定义的单元格网格线的类型变量
使用公式描述则为:
Border(left=左边线样式,right=右连线样式,top=上边线样式,bottom=下边线样式)
'''
# (8)cell.fill 设置单元格的填充样式
fill=openpyxl.styles.PatternFill(fill_type='darkGray', start_color='FFFFFFFF', end_color='8470FF')
cell1.fill=fill
'''
# 1.创建填充对象
fill=PatternFill(fill_type=None, start_color='FFFFFFFF', end_color='FF000000')
# 2.参数解读
fil type:
'None'(不填充)/
'solid'(实心填充)/
'darkGray'(75%灰色)
'mediumGray'(50%灰色)/
'ightGray'(25%灰色)/
'gray125'(12.5%灰色)
'gray0625'(6.25%灰色)/
'darkHorizonta”(水平条纹)/
'darkVerical'(垂直条纹)/
'darkDown'(逆对角线条纹)/
'darkUp'(对角线条纹)/
'darkGrid'(对角线剖面线)/
'darkTrelis'(粗对角线剖面线)/
'ightHorizonta'(细水平条纹)/
'lightvertical'(细垂直条纹)/
'ightDown'(细逆对角线条纹)/
'ightUp'(细对角线条纹)/
"ightGrid'(细水平剖面线)/
'ightTrellis'(细对角线剖面线)
start_color/fgColor:背景颜色 RGB转HEX
end_color/bgColor:图案颜色 RGB转HEX
RGB转HEX 指的一个RGB转HEX的色号转换网站,比如:
https://www.jyshare.com/front-end/55/
'''
# (9)cell对象.protection=变量 设置单元格的锁定和隐藏属性
protection=openpyxl.styles.Protection(locked=True,hidden=True)
cell1.protection=protection
open_xls.save('./Excel/单元格对象.xlsx')
def P1_4(self): # 该功能为以后自动读取并创建Excel表格和设置PyQt5中TabelWidget中单元格属性创造了条件
xlsx=openpyxl.load_workbook('./Excel/测试读取单元格的属性.xlsx')
sheet=xlsx.active
# 1.读取单元格边框线型
print('1.读取单元格边框线型'.center(20,'-'))
b2=sheet['b2']
border1=b2.border
print(f'左边框线形:{border1.left.border_style}')
print(f'右边框线形:{border1.right.border_style}')
print(f'上边框线形:{border1.top.border_style}')
print(f'下边框线形:{border1.bottom.border_style}')
# 2.读取单元格边框颜色
print('2.读取单元格边框颜色'.center(20, '-'))
d4=sheet['d4']
border2 = d4.border
print(f'左边框颜色:{border2.left.color}')
print(f'右边框颜色:{border2.right.color}')
print(f'上边框颜色:{border2.top.color}')
print(f'下边框颜色:{border2.bottom.color}')
# 3.读取单元格填充
print('3.读取单元格填充'.center(20, '-'))
f6=sheet['f6']
if f6.fill:
print("填充的类型:", f6.fill.fill_type)
print("前景色:", f6.fill.start_color.rgb)
print("背景色:", f6.fill.end_color.rgb)
else:
print("没有填充")
# 4.读取单元格的字体信息
print('4.读取单元格的字体信息'.center(20, '-'))
h8=sheet['h8']
font = h8.font
print(f"字体名称: {font.name}")
print(f"字体大小: {font.size}")
print(f"字体粗体: {font.bold}")
print(f"字体斜体: {font.italic}")
print(f"字体下划线: {font.underline}")
print(f"字体颜色: {font.color}")
# 5.工作表的打印属性
print('5.工作表的打印属性'.center(20, '-'))
print(sheet.page_setup.paperSize)
print(sheet.print_area)
print(sheet.print_title_cols)
print(sheet.print_title_rows)
pass
app = QApplication(sys.argv)
w = MyWin()
w.show()
app.exec()
'''
学习openxl库的诀窍
1.创建、保存文件
2.弄清楚工作表的属性,单元格的属性有哪些,方法有哪些就可以了
'''
学习代码:openpyxl_1
最新推荐文章于 2024-11-27 21:10:33 发布