简体   繁体   English

Python win32com select 范围

[英]Python win32com select range

I need to select a range of values from a workbook_A and copy them to another workbook_B.我需要 select 工作簿_A 中的一系列值并将它们复制到另一个工作簿_B。 The issue is that i need to copy ONLY the values while preserving the formatting of workbook_B.问题是我只需要复制值,同时保留 workbook_B 的格式。

I do not understand why it give me a error "com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', 'Select method of Range class failed', 'xlmain11.chm', 0, -2146827284), None)"我不明白为什么它会给我一个错误“com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', 'Select method of Range class failed', 'xlmain11.chm', 0, -2146827284 ), 没有任何)”

Here the code这里的代码

import pandas as pd
import win32com
from win32com.client import Dispatch,constants

wb_A = r"A.xlsx"
wb_B_Template = r"B_Template_Preformatted.xlsx"

xlapp = win32com.client.gencache.EnsureDispatch('Excel.Application')
xlapp.Visible= False
xlapp.DisplayAlerts = False

cars = {'Brand': ['Honda','Toyota','Ford','Audi'],
        'Price': [0.23, 0.35, 0.43, 0.57]}

df_cars = pd.DataFrame(cars, columns = ['Brand', 'Price'])
df_cars.to_excel(wb_A, "Table")

wb_origin = xlapp.Workbooks.Open(wb_A)
wb_destination = xlapp.Workbooks.Open(wb_B_Template)

wb_origin.Worksheets("Table").Range("A1:C5").Select()
xlapp.Selection.Copy(Destination=wb_destination.Worksheets("Tabelle").Range("A1"))

wb_origin.Close()
wb_destination.Close(SaveChanges=True)

xlapp.Quit()
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Add()
ws = wb.Worksheets("Sheet1")
ws.Range("A1").Value = 1
ws.Range("A2").Value = 2
ws.Range("A1:A2").AutoFill(ws.Range("A1:A10"),win32.constants.xlFillDefault)
wb.SaveAs('autofill_cells.xlsx')
excel.Application.Quit()

Check this out看一下这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM