Python使用win32com.client()和load_workbook()追加写入excle

win32com.client()和load_workbook()均可以实现在原有excle文件中写入内容,在实际使用中win32com.client()使用的方法类似VBA来模拟用户进行操作,运行速度不如load_workbook()。

1.win32com.client()写入excle

win32com.client在操作完excle后一般要及时关闭

所以wb.close()是必须的

import win32com.client as win32

allow_time_maxrows_flag = 3  #行数指针

excel = win32.DispatchEx('Excel.Application')  # 这个是必备的,
excel.Visible = False  # 是否可视化
excel.DisplayAlerts = False  # 禁止弹窗-不显示警告信息
wb = excel.Workbooks.Open(r"D:\Users\WYN\Desktop\Demo2\Data\06Cr19Ni10_t=1_DZ111\06Cr19Ni10_t=1_DZ111.xlsx",
                          ReadOnly=False)
ws = wb.Worksheets[0]  # sheet表名:wb.Sheets[0].Name
ws.Cells((allow_time_maxrows_flag[row_name] + 3), 1).Value = tableData_read.iloc[row_name, 0]
ws.Cells((allow_time_maxrows_flag[row_name] + 3), 2).Value = "\n".join(
    [str(tableData_read.iloc[row_name, 1]), str(tableData_read.iloc[row_name, 2])])
ws.Cells((allow_time_maxrows_flag[row_name] + 3), 3).Value = tableData_read.iloc[row_name, 3]
ws.Cells((allow_time_maxrows_flag[row_name] + 3), 4).Value = tableData_read.iloc[row_name, 4]
ws.Cells((allow_time_maxrows_flag[row_name] + 3), 5).Value = tableData_read.iloc[row_name, 5]
ws.Cells((allow_time_maxrows_flag[row_name] + 3), 6).Value = tableData_read.iloc[row_name, 6]
ws.Cells((allow_time_maxrows_flag[row_name] + 3), 7).Value = tableData_read.iloc[row_name, 7]
ws.PageSetup.PrintArea = 'A1:H%s' % (allow_time_maxrows_flag[row_name] + 3)  # 设置打印区域

wb.Save()  # 保存
wb.SaveAs(r"D:\Users\WYN\Desktop\Demo2\Data\06Cr19Ni10_t=1_DZ111\aaa.xls")  # 另存为

wb.Close(False)  # 关闭,如果你想看到excel温馨的提示,括号里就别填东西
excel.quit()  # 关闭excle操作环境

2.load_workbook()写入excle

openpyxl有workbook()和load_workbook()两种方法写入excle。workbook()用于创建新的excle,load_workbook()适用于在原有的excle文件中操作。

两种方法区别如下图

Python使用win32com.client()和load_workbook()追加写入excle_第1张图片

import openpyxl

workbook = openpyxl.load_workbook(path_name_cailiao_path_list[row_name])  # 这里用的相对路径,这是workbook,工作簿
worksheet = workbook[workbook.sheetnames[0]]  # 获得工作簿的所有工作表,将第一个工作表作为工作地
worksheet.cell(row=(allow_time_maxrows_flag[row_name] + 3), column=1, value=tableData_read.iloc[row_name, 0])
worksheet.cell(row=(allow_time_maxrows_flag[row_name] + 3), column=2,
               value="\n".join([str(tableData_read.iloc[row_name, 1]), str(tableData_read.iloc[row_name, 2])]))
worksheet.cell(row=(allow_time_maxrows_flag[row_name] + 3), column=3, value=tableData_read.iloc[row_name, 3])
worksheet.cell(row=(allow_time_maxrows_flag[row_name] + 3), column=4, value=tableData_read.iloc[row_name, 4])
worksheet.cell(row=(allow_time_maxrows_flag[row_name] + 3), column=5, value=tableData_read.iloc[row_name, 5])
worksheet.cell(row=(allow_time_maxrows_flag[row_name] + 3), column=6, value=tableData_read.iloc[row_name, 6])
worksheet.cell(row=(allow_time_maxrows_flag[row_name] + 3), column=7, value=tableData_read.iloc[row_name, 7])
worksheet.print_area = 'A1:H%s' % (allow_time_maxrows_flag[row_name] + 3)  # 设置打印区域
workbook.save(path_name_cailiao_path_list[row_name])

3.使用win32com.client()写入excle时excle进程不能关闭的解决方法

def close_excel_by_force(self, excel):
    import win32process
    import win32gui
    import win32api
    import win32con
    import time

    # Get the window's process id's
    hwnd = excel.Hwnd
    t, p = win32process.GetWindowThreadProcessId(hwnd)
    # 可以去掉 nicely to close 和 time.sleep 两步,直接 force close
    # Ask window nicely to close
    win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)
    # Allow some time for app to close
    time.sleep(10)
    # If the application didn't close, force close
    try:
        handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, p)
        if handle:
            win32api.TerminateProcess(handle, 0)
            win32api.CloseHandle(handle)
    except:
        pass

在执行完此函数后,可以删除excle

# 删除读取是创建的excle
del excle

参考连接:

(13 封私信 / 24 条消息) py的openpyxl库,load_workbook()和Workbook()有什么不一样? - 知乎 (zhihu.com)

你可能感兴趣的:(PyQt,python,excel)