我正在嘗試從工作簿1中復制特定行,并將其追加到工作簿2中的現有數據中。從工作簿 1 中復制突出顯示的行,并將它們追加到“March”下方的工作簿 2 中到目前為止,我成功地復制并粘貼了范圍,但有兩個問題:1.單元格是移位的2.缺少百分比(公式),只留下數值。在此處查看結果import openpyxl as xlsource = r"C:\Users\Desktop\Test_project_20200401.xlsx"wbs = xl.load_workbook(source)wbs_sheet = wbs["P2"] #selecting the sheetdestination = r"C:\Users\Desktop\Try999.xlsx"wbd = xl.load_workbook(destination)wbd_sheet = wbd["A3"] #select the sheetrow_data = 0for row in wbs_sheet.iter_rows(): for cell in row: if cell.value == "Yes": row_data += cell.rowfor row in wbs_sheet.iter_rows(min_row=row_data, min_col = 1, max_col=250, max_row = row_data+1): wbd_sheet.append((cell.value for cell in row)) wbd.save(destination)有沒有人知道我該如何解決這個問題?任何反饋/解決方案都會有所幫助!謝謝!
2 回答

MMTTMM
TA貢獻1869條經驗 獲得超4個贊
我認為min_col應該= 0
范圍(“A1”)。公式(在 VBA 中)獲取公式。范圍(“A1”)。值(在 VBA 中)獲取值。
因此,請嘗試在蟒蛇中使用 .公式
(感謝:從單元格中獲取公式 - VBA ...如果這有效)

四季花海
TA貢獻1811條經驗 獲得超5個贊
只是想在這里添加我自己的解決方案。
我所做的是循環訪問列并應用“cell.number_format= '0%',這會將您的單元格值轉換為百分比。
for col in ws.iter_cols(min_row=1, min_col=2, max_row=250, max_col=250): for cell in col: cell.number_format = '0%'
更多信息可以在這里找到: https://openpyxl.readthedocs.io/en/stable/_modules/openpyxl/styles/numbers.html
添加回答
舉報
0/150
提交
取消