Python 自动化处理Excel和Word实现自动办公

发布时间:2025-03-18 02:47

人工智能自动化处理重复任务,解放了生产力,如Excel的各种自动化公式 #生活知识# #生活感悟# #科技生活变迁# #科技改变工作方式#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

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

81

82

83

84

85

86

87

88

89

90

91

92

93

import openpyxl as xl

from openpyxl.chart import LineChart, Reference

import win32com.client

import PIL

from PIL import ImageGrab, Image

import os

import sys

from docx.shared import Cm

from docxtpl import DocxTemplate, InlineImage

from docx.shared import Cm, Inches, Mm, Emu

import random

import datetime

import matplotlib.pyplot as plt

workbook = xl.load_workbook('Book1.xlsx')

sheet_1 = workbook['Sheet1']

for row in range(2, sheet_1.max_row + 1):

    current = sheet_1.cell(row, 2)

    voltage = sheet_1.cell(row, 3)

    power = float(current.value) * float(voltage.value)

    power_cell = sheet_1.cell(row, 1)

    power_cell.value = power

values = Reference(sheet_1, min_row = 2, max_row = sheet_1.max_row, min_col = 1, max_col = 1)

chart = LineChart()

chart.y_axis.title = 'Power'

chart.x_axis.title = 'Index'

chart.add_data(values)

sheet_1.add_chart(chart, 'e2')

workbook.save('Book1.xlsx')

input_file = "C:/Users/.../Book1.xlsx"

output_image = "C:/Users/.../chart.png"

operation = win32com.client.Dispatch("Excel.Application")

operation.Visible = 0

operation.DisplayAlerts = 0

workbook_2 = operation.Workbooks.Open(input_file)

sheet_2 = operation.Sheets(1)

for x, chart in enumerate(sheet_2.Shapes):

    chart.Copy()

    image = ImageGrab.grabclipboard()

    image.save(output_image, 'png')

    pass

workbook_2.Close(True)

operation.Quit()

template = DocxTemplate('template.docx')

table_contents = []

for i in range(2, sheet_1.max_row + 1):

    table_contents.append({

        'Index': i-1,

        'Power': sheet_1.cell(i, 1).value,

        'Current': sheet_1.cell(i, 2).value,

        'Voltage': sheet_1.cell(i, 3).value

        })

image = InlineImage(template,'chart.png',Cm(10))

context = {

    'title': 'Automated Report',

    'day': datetime.datetime.now().strftime('%d'),

    'month': datetime.datetime.now().strftime('%b'),

    'year': datetime.datetime.now().strftime('%Y'),

    'table_contents': table_contents,

    'image': image

    }

template.render(context)

template.save('Automated_report.docx')

网址:Python 自动化处理Excel和Word实现自动办公 https://www.yuejiaxmz.com/news/view/829633

相关内容

Python自动化办公技巧分享
Python办公自动化:解锁高效工作流程,掌握文档处理的艺术
Python处理办公自动化的10大场景
Task 03 python自动化之word操作
Python与xlwings黄金组合处理Excel各种数据和自动化任务
Python自动化任务
避坑指南!细说Python自动化办公的5大缺点
大家都在聊的自动化办公到底是什么?
办公自动化用什么软件编程最好
Word/Excel/PPT/PS/移动办公Office 5合1无师自通【全本

随便看看