私はPython newbです。私は非常に時間のかかるプロジェクトを自動化するプロジェクトに取り組んでいます。私はopenpyxlを使用して.xlsxにアクセスし、最終的に距離と方向のベアリングをarcpy/arcgisと一緒に使用するように変換します。つまり、Python 2.7を使用しています。データにアクセスして最初の変更を行うことはできますが、書き込みコマンドをループに統合することはできません。現在、最後の行のデータを新しい.xlsxの指定された範囲にあるすべてのセルに保存します。ここに私のコードは次のとおりです。PythonとOpenpyxlを使って.xlsxをループするが、ループは最後の行のデータのみを保存する
#Importing OpenPyXl and loads the workbook and sheet
import openpyxl
wb = openpyxl.load_workbook('TESTVECT.xlsx')
ws = wb.get_sheet_by_name('TEST')
#allows to save more than once
write_only = False
cell_range = ws['C']
#sorts through either the rows/columns and slices the required string
maxRow = ws.max_row + 1
for row in range(2, maxRow):
parID = ws['A' + str(row)].value
Lline = ws['B' + str(row)].value
Vect = ws['C' + str(row)].value
print parID, Lline, Vect
trash, keep = Vect.split("C")
#This part save the very last row to all rows in available columns
#need a way to integrate the save functionality so each row is unique
for rowNum in range(2, maxRow):
ws.cell(row=rowNum, column=3).value = keep
for rowNum in range (2, maxRow):
ws.cell(row=rowNum, column=1).value = parID
for rowNum in range (2, maxRow):
ws.cell(row=rowNum, column=2).value = Lline
#Only prints the very last keep entry from the .xlsx
print keep
print "all done"
#Saving does not write all of the the 'keep, parID, and Lline' records
#There is an issue with the for loop and integrating the write portion of
#the code.
wb.save('TESTMONKEYVECT.xlsx')
誰かが私に私が書き込み処理と間違っをやっているかについていくつかのポインタを与えてくださいすることができ、私は変更が行われた後、各行が独自のデータを保持する必要があります。
ありがとう、
これは完全に機能します。どうもありがとうございます!この小さなスクリプトは、最初に10,000を超えるレコードを更新して、定期的なメンテナンス中に時間を節約できます。これは大きな助けとなりました。今後のプロジェクトでもこの構文を使用します。ありがとうございました。 –