测试环境
Python 3.6.2
代码实现
非多线程场景下使用
新建并保存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
|
import win32com.client from win32api import RGB def save_something_to_excel(result_file_path): excel_app = win32com.client.Dispatch( 'Excel.Application' ) excel_app.Visible = False # 设置进程界面是否可见 False表示后台运行 excel_app.DisplayAlerts = False # 设置是否显示警告和消息框 book = excel_app.Workbooks.Add() # 添加Excel工作簿 sheet = excel_app.Worksheets( 1 ) # 获取第一个Sheet sheet.name = '汇总统计' # 设置Sheet名称 sheet.Columns.ColumnWidth = 10 # 设置所有列列宽 sheet.Columns( 1 ).ColumnWidth = 20 # 设置第1列列宽 sheet.Rows.RowHeight = 15 # 设置所有行高 sheet.Rows( 1 ).RowHeight = 20 # 设置第一行行高 usedRange = sheet.UsedRange # 获取sheet的已使用范围 rows = usedRange.Rows.Count # 获取已使用范围的最大行数,初始值为 1 cols = usedRange.Columns.Count # 获取已使用范围的最大列数,初始值为 1 print (rows, cols) # 输出 1 1 usedRange.Rows.RowHeight = 30 # 设置已使用范围内的行高 usedRange.Columns.ColumnWidth = 30 # 设置已使用范围内的列宽 # do something ... row_index = 1 for index, item in enumerate ([ '日期' , '请求方法' , 'URL' , '调用次数' ]): # 单元格赋值 sheet.Cells(row_index, col_index).Value = 目标值 row_index, col_index 起始值为1 sheet.Cells(row_index, index + 1 ).Value = item row_index + = 1 # do something else ... usedRange = sheet.UsedRange rows = usedRange.Rows.Count cols = usedRange.Columns.Count print (rows, cols) # 输出 1 4 sheet.Cells( 1 , 2 ).Font.Size = 29 # 设置单元格字体大小 sheet.Cells( 1 , 2 ).Font.Bold = True # 字体是否加粗 True 表示加粗,False 表示不加粗 sheet.Cells( 2 , 2 ).Font.Name = "微软雅黑" # 设置字体名称 # sheet.Cells(2, 2).Font.Color = RGB(0, 0, 255) # 设置字体颜色 # 不起作用 sheet2 = excel_app.Worksheets.Add() # 添加Sheet页 sheet2.Activate # 设置默认选中的sheet为sheet2 sheet3 = excel_app.Worksheets.Add() #注意,Move操作,会将被移动的表单(本例中的sheet)设置为默认选中状态,也就是说覆盖 sheet.Activate所做的变更 sheet.Move(sheet3, None ) # 将sheet移动到sheet3之前 book.SaveAs(result_file_path) # 注意:结果文件路径必须是绝对路径 book.Close() # 关闭工作簿 excel_app.Quit() # 退出 if __name__ = = '__main__' : save_something_to_excel( 'D:\\codePojects\\logStatistics\\result\\result.xlsx' ) |
了解更多API,可以查看参考连接
读取现有EXCEL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import win32com.client def read_something_from_excel(excel_file_path): excel_app = win32com.client.Dispatch( 'Excel.Application' ) excel_app.Visible = False excel_app.DisplayAlerts = False book = excel_app.Workbooks. Open (result_file_path, False , True , None , None ) # 打开工作簿 # do something ... sheet = excel_app.Worksheets( 1 ) print (sheet.name) print (sheet.Cells( 1 , 1 ).Value) book.SaveAs(result_file_path) # 注意:结果文件路径必须是绝对路径 book.Close() # 关闭工作簿 excel_app.Quit() # 退出 if __name__ = = '__main__' : read_something_from_excel( 'D:\\codePojects\\logStatistics\\result\\result.xlsx' ) |
多线程场景下使用
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
|
import threading import win32com.client import pythoncom def save_something_to_excel(result_file_path): pythoncom.CoInitialize() excel_app = win32com.client.DispatchEx( 'Excel.Application' ) # excel_app = win32com.client.Dispatch('Excel.Application') excel_app.Visible = False excel_app.DisplayAlerts = False book = excel_app.Workbooks.Add() sheet = excel_app.Worksheets( 1 ) sheet.name = '汇总统计' row_index = 1 for index, item in enumerate ([ '日期' , '请求方法' , 'URL' , '调用次数' ]): sheet.Cells(row_index, index + 1 ).Value = item row_index + = 1 book.SaveAs(result_file_path) book.Close() excel_app.Quit() pythoncom.CoUninitialize() # 释放资源 if __name__ = = '__main__' : for i in range ( 3 ): file_path = 'D:\\codePojects\\logStatistics\\result\\result%s.xlsx' % i thread = threading.Thread(target = save_something_to_excel, args = (file_path,)) thread.start() |
说明:
- 如果不添加以下代码行:
1
|
pythoncom.CoInitialize() |
会报错,如下:
1
|
pywintypes.com_error: ( - 2147221008 , '尚未调用 CoInitialize。' , None , None ) |
- 建议使用
1
|
excel_app = win32com.client.DispatchEx( 'Excel.Application' ) |
替代
1
|
# excel_app = win32com.client.Dispatch('Excel.Application') |
实践发现,多线程的情况下,使用Dispatch
会出现报错,原因似乎是Dispatch
若发现进程已经存在的话,就不会创建新的进程。若不创建新的进程,有些操作会有冲突,可能会影响到已经打开的文件。
参考连接
https://learn.microsoft.com/zh-cn/office/vba/api/excel.font.color
https://blog.csdn.net/qq_25176745/article/details/125085819
到此这篇关于Python 基于win32com客户端实现Excel操作的详细过程的文章就介绍到这了,更多相关Python Excel操作内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://www.cnblogs.com/shouke/archive/2023/04/29/17277611.html