0
Pythonとwin32comを使用:Python:以前のセルの数式に従ってExcelでセルを自動入力
私はセルファイルをA5:A54のExcelファイルに作成しています。私はA55:A61のセルに同じ式に従って値を書き込もうとしています。私はオンラインで検索し、これが見つかりました:リンクから http://pythonexcels.com/python-excel-mini-cookbook/
引用:「このスクリプトは、その後A10を通じて細胞の残りの列を自動入力、セルA1とA2のデータを調べるために、Excelのオートフィル機能を使用しています」 をリンクからコード:
from win32com.client import Dispatch
from win32com.client import constants
import os
from Tkinter import Tk
from tkFileDialog import askopenfilename
import win32com.client as win32
xlApp = Dispatch("Excel.Application")
...
print "Choose the excel file that you want to copy the data to"
Tk().withdraw()
filename_to = askopenfilename()
xlBook_to = xlApp.Workbooks.Open(filename_to)
xlApp.Visible=1
xlSheet_to_final = xlBook_to.Sheets(1)
xlBook_to.Activate()
xlSheet_to_final.Range("A53:A54").Select()
xlSheet_to_final.Range("A53:A54").AutoFill(xlSheet_to_final.Range("A55:A61"),win32.constants.xlFillDefault)
をそして、私はこのエラーを取得する:
#
# Autofill cell contents
#
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()
は、だから私は私のコードでそれを試してみました
File "C:\Users\<user>\excel_test.py", line 74, in <module>
xlSheet_to_final.Range("A53:A54").AutoFill(xlSheet_to_final.Range("A55:A61"),win32.constants.xlFillDefault)
File "C:\Python27\Lib\site-packages\win32com\gen_py\00020813-0000-0000-C000-000000000046x0x1x8\Range.py", line 66, in AutoFill
, Type)
File "C:\Python27\Lib\site-packages\win32com\client\__init__.py", line 459, in _ApplyTypes_
self._oleobj_.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args),
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Excel', u'AutoFill method of Range class failed', u'xlmain11.chm', 0, -2146827284), None)
私はそれを修正するにはどうすればよいですか?前のセルの数式を次のセルに適用するにはどうすればよいですか?
私は取得しています "はImportErrorを:いいえモジュールの名前win32.constants" 試してみてください。 – user2653179
しかし、あなたの答えは、私が問題を解決するのを助けました:最初から2番目の部分で範囲を開始する必要があります(オートフィルで) "xlSheet_to_final.Range(" A53:A54 ")。オートフィル(xlSheet_to_final.Range(" A53:A61 ")、xlFillDefault)"を参照してください。ありがとう! – user2653179
よろしくお願いします。 Typo、それはおそらくwin32com.constantsであるはずです。 – Schollii