python 对excel的函数操作_自动化报表(3)

1、

# 方法1
# openpyxl模块中的Comment()函数,给单元格添加批注

import pandas as pd
from openpyxl import Workbook
from openpyxl.comments import Comment

# 方法1
# openpyxl模块中的Comment()函数,给单元格添加批注

wb = Workbook()
ws = wb.active

# 逗号前表示具体批注内容
# 逗号后表示批注者
comment = Comment("这是一条新建的批注20220122", "jack")

# 用来设置批注框的宽和高
comment.width = 300
comment.height = 100

# 给C6单元格添加批注
ws["C6"].comment = comment
wb.save("C:/Users/Administrator/Desktop/999/202201221055.xlsx")

2、

# python代码中的openpyxl模块,设置保护工作表

import pandas as pd
from openpyxl import Workbook
from openpyxl.comments import Comment

# python代码中的openpyxl模块,设置保护工作表

wb = Workbook()
ws = wb.active

ws.protection.password = '123456'
wb.save("C:/Users/Administrator/Desktop/999/202201221155.xlsx")

3、

# python代码中的openpyxl模块,设置保护工作簿

import pandas as pd
from openpyxl import Workbook
from openpyxl.comments import Comment

# python代码中的openpyxl模块,设置保护工作簿

wb = Workbook()
ws = wb.active

wb.security.workbookPassword = '123456'
wb.security.lockStructure = True
wb.save("C:/Users/Administrator/Desktop/999/202201221414.xlsx")

4、

# 设置保护工作表 和 设置保护工作簿,同时做保护。

  

import pandas as pd
from openpyxl import Workbook
from openpyxl.comments import Comment

# 设置保护工作表 和 设置保护工作簿,同时做保护。


wb = Workbook()
ws = wb.active

# 设置保护工作表
ws.protection.password = '123456'
# 设置保护工作簿
wb.security.workbookPassword = '123456'
wb.security.lockStructure = True
wb.save("C:/Users/Administrator/Desktop/999/202201221423.xlsx")

5、

#  冻结窗格,冻结首行,冻结首列
# 要实现冻结前两行和前两列的效果

    

import pandas as pd
from openpyxl import Workbook
from openpyxl.comments import Comment

#  冻结窗格,冻结首行,冻结首列
# 要实现冻结前两行和前两列的效果


wb = Workbook()
ws = wb.active

# 如果要实现冻结首行,则只需要把上面冻结窗格代码等于A2。
ws.freeze_panes = 'A2'
wb.save("C:/Users/Administrator/Desktop/999/202201221522.xlsx")
# 冻结首列,则只需要把上面冻结窗格代码等于B1。
ws.freeze_panes = 'B1'
wb.save("C:/Users/Administrator/Desktop/999/202201221609.xlsx")
# 在Python中,要实现冻结前两行和前两列的效果,可以使用如下代码。
ws.freeze_panes = 'C3'
wb.save("C:/Users/Administrator/Desktop/999/202201221521.xlsx")


6、

# openpyxl 创建一个折线图

   

import pandas as pd
from openpyxl import Workbook
from openpyxl.chart import Reference, LineChart


# openpyxl 创建一个折线图

# 创建一个工作簿
wb = Workbook()
ws = wb.active

rows = [["月份", "注册人数"],
        ["1月", 866],
        ["2月", 2335],
        ["3月", 5710],
        ["4月", 6482],
        ["5月", 6120],
        ["6月", 1605],
        ["7月", 3813],
        ["8月", 4428],
        ["9月", 4631],
        ]

for row in rows:
    ws.append(row)

#   建立一个空折线图坐标系
c1 = LineChart()
# 向空坐标系中添加数据
data = Reference(ws, min_col=2, max_col=2, min_row=1, max_row=10)
# titles_from_data=True 的作用是使表头不计入数据
c1.add_data(data, titles_from_data=True)

# 对图表元素进行设置
c1.title = "1-9月注册人数"
c1.style = 1  # 图表样式类型(1-48)
c1.y_axis.title = "注册人数"
c1.x_axis.title = "月份"

# 将图标添加到工作簿中A8单元格位置
ws.add_chart(c1, "D1")

wb.save("C:/Users/Administrator/Desktop/999/202201242006.xlsx")

  6.1、

echarts 折线图 轴点从y轴开始_python学习笔记 - 绘制折线图与条形图_Strive追逐者的博客-CSDN博客https://blog.csdn.net/weixin_29229261/article/details/112419027

7、openpyxl 创建一个柱状图

        

import pandas as pd
from openpyxl import Workbook
from openpyxl.chart import Reference, BarChart


# openpyxl 创建一个柱状图

# 创建一个工作簿
wb = Workbook()
ws = wb.active

rows = [["月份", "注册人数"],
        ["1月", 866],
        ["2月", 2335],
        ["3月", 5710],
        ["4月", 6482],
        ["5月", 6120],
        ["6月", 1605],
        ["7月", 3813],
        ["8月", 4428],
        ["9月", 4631],
        ]

for row in rows:
    ws.append(row)

#   建立一个创建一个柱状图坐标系
c1 = BarChart()
# 向空坐标系中添加数据
data = Reference(ws, min_col=2, max_col=2, min_row=1, max_row=10)
# titles_from_data=True 的作用是使表头不计入数据
c1.add_data(data, titles_from_data=True)

# 对图表元素进行设置
c1.title = "1-9月注册人数"
c1.style = 1  # 图表样式类型(1-48)
c1.y_axis.title = "注册人数"
c1.x_axis.title = "月份"

# 将图标添加到工作簿中A8单元格位置
ws.add_chart(c1, "D1")

wb.save("C:/Users/Administrator/Desktop/999/202201251139.xlsx")

   

8、# openpyxl 创建一个面积图

     

import pandas as pd
from openpyxl import Workbook
from openpyxl.chart import Reference, AreaChart


# openpyxl 创建一个面积图

# 创建一个工作簿
wb = Workbook()
ws = wb.active

rows = [["月份", "注册人数"],
        ["1月", 866],
        ["2月", 2335],
        ["3月", 5710],
        ["4月", 6482],
        ["5月", 6120],
        ["6月", 1605],
        ["7月", 3813],
        ["8月", 4428],
        ["9月", 4631],
        ]

for row in rows:
    ws.append(row)

#   创建一个面积图坐标系
c1 = AreaChart()
# 向空坐标系中添加数据
data = Reference(ws, min_col=2, max_col=2, min_row=1, max_row=10)
# titles_from_data=True 的作用是使表头不计入数据
c1.add_data(data, titles_from_data=True)

# 对图表元素进行设置
c1.title = "1-9月注册人数"
c1.style = 1  # 图表样式类型(1-48)
c1.y_axis.title = "注册人数"
c1.x_axis.title = "月份"

# 将图标添加到工作簿中A8单元格位置
ws.add_chart(c1, "D1")

wb.save("C:/Users/Administrator/Desktop/999/202201251524.xlsx")

9、# openpyxl 创建一个散点图 (还没搞定)

10、 

     10.1、 openpyxl 创建一个气泡图

     10.2、 为什么必须加上区间(列)?

       

import pandas as pd
from openpyxl import Workbook
from openpyxl.chart import Reference, BubbleChart, Series

# openpyxl 创建一个气泡图

# 创建一个工作簿
wb = Workbook()
ws = wb.active

# 为什么必须加上区间
rows = [["月份", "注册人数", "区间"],
        ["1月", 866, 10],
        ["2月", 2335, 20],
        ["3月", 5710, 50],
        ["4月", 6482, 60],
        ["5月", 6120, 60],
        ["6月", 1605, 10],
        ["7月", 3813, 30],
        ["8月", 4428, 40],
        ["9月", 4631, 40],
        ]

for row in rows:
    ws.append(row)

#   创建一个气泡图坐标系
c1 = BubbleChart()
# 向空坐标系中添加数据
xvalues = Reference(ws, min_col=1, min_row=1, max_row=10)  # x值
yvalues = Reference(ws, min_col=2, min_row=2, max_row=10)  # y值
size = Reference(ws, min_col=3, min_row=1, max_row=10)  # size 值
series = Series(values=yvalues, xvalues=xvalues, zvalues=size)
c1.series.append(series)

# 对图表元素进行设置
c1.title = "1-9月注册人数"
c1.style = 13  # 图表样式类型(1-48)
c1.y_axis.title = "注册人数"
c1.x_axis.title = "月份"

# 将图标添加到工作簿中A8单元格位置
ws.add_chart(c1, "e1")

wb.save("C:/Users/Administrator/Desktop/999/202201251619.xlsx")

11、  

     11.1、图表布局设置

     11.2、对图表进行布局设置用到下面的一段代码

              chart1.layout = Layout(ManualLayout(x=0.2, y=0.1, h=0.6, w=0.8))

from openpyxl import Workbook
from openpyxl.chart.layout import Layout, ManualLayout
from openpyxl.chart.legend import Legend
from openpyxl.chart import BarChart, Reference, Series
from copy import deepcopy

# 图表布局设置

wb = Workbook()  # 建立工作表
ws = wb.active  # 激活工作表
for i in range(10):
    ws.append([i])

values = Reference(ws, min_col=1, min_row=1, max_col=1, max_row=10)  # 数据引用
chart = BarChart()  # 建立坐标系
chart.add_data(values)  # 添加数据
ws.add_chart(chart, "C2")  # 添加图表

chart1 = deepcopy(chart)
# 对图表进行布局设置用到下面的一段代码
chart1.layout = Layout(ManualLayout(x=0.2, y=0.1, h=0.6, w=0.8))
#  图表标题放在那个位置
chart1.legend = Legend(legendPos='b')
ws.add_chart(chart1, "C18")  # 添加图表

wb.save("C:/Users/Administrator/Desktop/999/202201251710.xlsx")

12、 如何将图片插入excel中

      

from openpyxl import Workbook
from openpyxl.drawing.image import Image

# 如何将图片插入excel中
wb = Workbook()
ws = wb.active

# 指明要导入图片的文件路径
img = Image("C:/Users/Administrator/Desktop/999/2020.jpg")
# 将图片添加到ws中
ws.add_image(img, "c3")

# 对图片的高和宽进行设置

newsize = (190, 190)
img.width, img.height = newsize
wb.save("C:/Users/Administrator/Desktop/999/202201251833.xlsx")

13、  python纵向拼接excel,excel字段相同保持相同

          13.1、用for循环拼接,代码中 定义一个空的二维数组,也是可以的

           df_o = pd.DataFrame()

     

                    

import pandas as pd
import os

print(os.getcwd())

# python纵向拼接excel,excel字段相同保持相同
# 查看文件下的excel
name_list = os.listdir("C:/Users/Administrator/Desktop/999/777/")
# print(name_list)
#   创建一个相同结构的空二维数组DataFrame
df_o = pd.DataFrame({'日期': [], '销量': []})
# print(df_o)
# 遍历读取每一个文件
for i in name_list:
    df = pd.read_excel("C:/Users/Administrator/Desktop/999/777/"+i)
    pd_v = pd.concat([df_o, df])
    df_o = pd_v
    # print(df_o)


print(df_o)

          13.2、单独读取文件拼接

     

import pandas as pd

# 读取两个excel文件
df1 = pd.read_excel("C:/Users/Administrator/Desktop/999/777/3月绩效_123.xlsx")
df2 = pd.read_excel("C:/Users/Administrator/Desktop/999/777/3月绩效_456.xlsx")

# 当两个文件的列表字段一样时,可以用concat函数直接合同(显示一列,列表字段)
#  可以多个excel,一起纵向拼接
pd_v = pd.concat([df2, df1])
print(pd_v)



   

14、

   python读取excel,按excel中的时间,再重新生产新的excel
   按月的维度,把exce才分成多份。

    

import pandas as pd
import os

print(os.getcwd())

# python读取excel,按excel中的时间,再重新生产新的excel
# 按月的维度,把exce才分成多份。
pd_re = pd.read_excel("C:/Users/Administrator/Desktop/999/202201271033.xlsx")
# print(pd_re)
pd_df = pd.DataFrame(pd_re)
# print(pd_df)
pd_df['月份'] = pd_df['日期'].apply(lambda x: x.month)
# print(pd_df)后

#  调用unique()函数是去重的意思。
for m in pd_df['月份'].unique():
    # 讲特定月份的值的数据筛选出来
    df_month = pd_df[pd_df['月份'] == m]
    # print(df_month)
    # 按月筛选出的数据进行保存
    df_month.to_excel("C:/Users/Administrator/Desktop/999/"+str(m)+"月销售日报_拆分.xlsx")


15、

16、

17、

18、

19、

20、

21、

22、

23、

24、

25、

26、

27、

28、

29、

30、

31、

32、

33、

34、

35、

36、

37、

38、

39、

40、

41、

42、

43、

44、

45、

46、

47、

48、

49、

50、

51、

52、

53、

54、

55、

56、

57、

58、

59、

60、

61、

62、

63、

64、

65、

66、

67、

68、

69、

70、

71、

72、

73、

74、

75、

76、

77、

78、

79、

80、

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值