import win32com.client as win32

1、安装包
pip install pypiwin32
pip install openpyxl 

2、代码
import os
from openpyxl import Workbook

dirname = os.path.abspath('.')
excel = win32.DispatchEx('Excel.Application')
excel.Visible = False
excel.DisplayAlerts = False

if os.path.isfile(dirname + '\\' + 'test2.xlsx'):
    os.remove(dirname + '\\' + 'test2.xlsx')

def write_table_upload(ws0, wb2, ws2, sheet_name):
    ws0.Copy(ws2)
    wb2.ActiveSheet.Name = sheet_name
    change_worksheet = wb2.Worksheets(sheet_name)
    change_worksheet.Range('D3').Value = 'D3_Value'
    change_worksheet.Range('D4').Value = 'D4_Value'
    change_worksheet.Range('D5').Value = 'D5_Value'
    change_worksheet.Range('F16').Value = 'F16_SQL'


dirname = dirname.replace('\\', '/')
if __name__ == '__main__':
    for sys_name in ('ECIF', 'IPS'):
        resource_file = dirname + '/' + sys_name + 'test1.xlsx'
        result_file = dirname + '/' + sys_name + 'test2.xlsx'
        print(result_file)
        print(result_file)
        if os.path.isfile(result_file):
            os.remove(result_file)
        if not os.path.exists(result_file) or not os.path.isfile(result_file):
            Workbook().save(result_file)
        wb0 = excel.Workbooks.Open(dirname + '\\' + 'test1.xlsx')
        ws0 = wb0.Worksheets('table_upload_model')
        wb2 = excel.Workbooks.Open(result_file)
        ws2 = wb2.Worksheets("Sheet")
        for sheet_name in (sys_name + 'aaa', sys_name + 'bbb'):
            write_table_upload(ws0=ws0, wb2=wb2, ws2=ws2, sheet_name=sheet_name)

        wb2.Save()
        wb0.Close()
        wb2.Close()
    excel.Application.Quit()

3、示例文件。截图不好截全,就如图就够用了。

pypiwin32 复制sheet 到另一个工作薄

相关文章:

  • 2022-03-01
  • 2021-12-29
  • 2022-12-23
  • 2021-09-14
  • 2021-10-22
  • 2022-12-23
  • 2021-06-03
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-29
  • 2021-07-14
  • 2021-10-04
  • 2022-12-23
  • 2022-01-05
相关资源
相似解决方案