from openpyxl import Workbook
import openpyxl
import os
# pip install openpyxl==2.6.4
# Copy range of cells as a nested list
# Takes: start cell, end cell, and sheet you want to copy from.
def copy_range(start_col, start_row, end_col, end_row, sheet):
range_selected = []
# Loops through selected Rows
for i in range(start_row, end_row + 1, 1):
# Appends the row to a RowSelected list
row_selected = []
for j in range(start_col, end_col + 1, 1):
row_selected.append(sheet.cell(row=i, column=j).value)
# Adds the RowSelected List and nests inside the rangeSelected
range_selected.append(row_selected)
return range_selected
# Paste range
# Paste data from copy_range into template sheet
def paste_range(start_col, start_row, end_col, end_row,
sheet_receiving, copied_data):
count_row = 0
for i in range(start_row, end_row + 1, 1):
count_col = 0
for j in range(start_col, end_col + 1, 1):
sheet_receiving.cell(row=i, column=j).value = \
copied_data[count_row][count_col]
count_col += 1
count_row += 1
def main():
# output sheet
output = Workbook()
output.save("result.xlsx")
output = openpyxl.load_workbook("result.xlsx") # Add file name
output_sheet = output.active
for i in range(2, 13):
input = openpyxl.load_workbook("{}.xlsx".format(str(i)),
data_only=True,
read_only=True)
sheets = input.sheetnames
sheet = input[sheets[1]] # select second sheet
# ==========================
# name
# ==========================
name_col_offset = 2
name_row_offset = 18
selected_range = copy_range(start_col=2,
start_row=3,
end_col=2 + name_col_offset,
end_row=3 + name_row_offset,
sheet=sheet)
paste_range(start_col=1,
start_row=-50 + 30 * i,
end_col=1 + name_col_offset,
end_row=-50 + 30 * i + name_row_offset,
sheet_receiving=output_sheet,
copied_data=selected_range)
# ==========================
# data
# ==========================
data_col_offset = 13
data_row_offset = 20
# handle month >=5 excel
if i >= 5:
start_col = 8
else:
start_col = 7
selected_range = copy_range(start_col=start_col,
start_row=3,
end_col=start_col + data_col_offset,
end_row=3 + data_row_offset,
sheet=sheet)
paste_range(start_col=4,
start_row=-50 + 30 * i,
end_col=4 + data_col_offset,
end_row=-50 + 30 * i + data_row_offset,
sheet_receiving=output_sheet,
copied_data=selected_range)
output.save("result.xlsx")
print str(i) + " is done"
if __name__ == '__main__':
myfile = "result.xlsx"
if os.path.isfile(myfile):
os.remove(myfile)
main()
批量提取excel固定行列到一個excel中去
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來捅位,“玉大人轧葛,你說我怎么就攤上這事⊥Р螅” “怎么了尿扯?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長焰雕。 經(jīng)常有香客問我衷笋,道長,這世上最難降的妖魔是什么矩屁? 我笑而不...
- 正文 為了忘掉前任辟宗,我火速辦了婚禮,結(jié)果婚禮上吝秕,老公的妹妹穿的比我還像新娘泊脐。我一直安慰自己,他們只是感情好郭膛,可當(dāng)我...
- 文/花漫 我一把揭開白布晨抡。 她就那樣靜靜地躺著,像睡著了一般则剃。 火紅的嫁衣襯著肌膚如雪耘柱。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼娄柳,長吁一口氣:“原來是場噩夢啊……” “哼寓辱!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起赤拒,我...
- 正文 年R本政府宣布,位于F島的核電站蛉拙,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏彻亲。R本人自食惡果不足惜孕锄,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望苞尝。 院中可真熱鬧畸肆,春花似錦、人聲如沸宙址。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽抡砂。三九已至大咱,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間注益,已是汗流浹背碴巾。 一陣腳步聲響...