Created
February 9, 2015 03:06
-
-
Save taojy123/63dac200a2577fe9d3b0 to your computer and use it in GitHub Desktop.
Python 读写 xlsx 文件操作
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from openpyxl import Workbook | |
from openpyxl.reader.excel import load_workbook | |
wb_new = Workbook() | |
wb = load_workbook('1.xlsx') | |
col1 = "C" | |
col2 = "A" | |
sheet1 = wb.get_sheet_by_name(wb.sheetnames[0]) | |
sheet2 = wb.get_sheet_by_name(wb.sheetnames[1]) | |
values1 = [] | |
i = 1 | |
while True: | |
if not sheet1[col1 + str(i)].value: | |
break | |
values1.append(sheet1[col1 + str(i)].value) | |
i += 1 | |
values2 = [] | |
i = 1 | |
while True: | |
if not sheet2[col2 + str(i)].value: | |
break | |
values2.append(sheet2[col2 + str(i)].value) | |
i += 1 | |
sheet3 = wb_new.get_sheet_by_name(wb_new.sheetnames[0]) | |
for n in range(len(values1)): | |
value = values1[n] | |
i = str(n + 1) | |
sheet3["A" + i].value = value | |
for n in range(len(values2)): | |
value = values2[n] | |
i = str(n + 1) | |
sheet3["B" + i].value = value | |
wb_new.save("2.xlsx") | |
print "ok!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment