在 Python 3.9 上。
所以這就是我想要做的。我幾乎每小時都在運行報告,并且在匯出到 excel 時沒有按照我需要的方式對其進行格式化。
我基本上是想找出一種從第二行開始的方法(因為我有標題)并將 =RIGHT(Ax, 10) 應用于 A 列中的每一行,例如 =RIGHT(A2, 10), =RIGHT (A3, 10), =RIGHT(A4, 10),依此類推,直到作業簿中的最后一行。每個作業簿都有任意數量的行,因為它會隨著時間的推移而變化(增加)。
import csv
import openpyxl
wb = openpyxl.Workbook()
ws = wb.active
with open(r'location\file.csv') as f:
reader = csv.reader(f, delimiter=',')
for row in reader:
ws.append(row)
wb.save(r'location\file.xlsx')
wb = openpyxl.load_workbook(r'location\file.xlsx')
sheet = wb['Sheet']
maxr = sheet.max_row
tuple(sheet[f'A2:{maxr}'])
sheet[f'A2:A{maxr}'] = f"=RIGHT(A2:A{maxr}, 10)"
wb.save(r'location\file.xlsx')
uj5u.com熱心網友回復:
代替
tuple(sheet[f'A2:{maxr}'])
sheet[f'A2:A{maxr}'] = f"=RIGHT(A2:A{maxr}, 10)"
和
for i in range(1,maxr 1):
sheet[f'A{i}'].value = sheet[f'A{i}'].value[-10:]
uj5u.com熱心網友回復:
盡量保持簡單。請嘗試以下操作。
更改代碼的這一部分
maxr = sheet.max_row
tuple(sheet[f'A2:{maxr}'])
sheet[f'A2:A{maxr}'] = f"=RIGHT(A2:A{maxr}, 10)"
到一個回圈
maxr = len(sheet['A'])
for curr_row in range(2, maxr 1):
curr_value = sheet.cell(row=curr_row, column=4).value
new_value = curr_value[-10:]
sheet.cell(row=curr_row, column=4) = new_value
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/341097.html
