Created
October 27, 2016 05:51
-
-
Save tengpeng/5646da293deee8364eb808001feb3376 to your computer and use it in GitHub Desktop.
This gist looks up variables' name by openpyxl in xlsx
This file contains 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
# coding: utf-8 | |
# In[1]: | |
import openpyxl | |
# In[2]: | |
wb = openpyxl.load_workbook('rsquare.xlsx') | |
var = openpyxl.load_workbook('x.xlsx') | |
# In[5]: | |
var_sheet = var.get_sheet_by_name('x') | |
key = [] | |
for i in var_sheet['C']: | |
key.append(i.value) | |
value = [] | |
for i in var_sheet['D']: | |
value.append(i.value) | |
var_dict = dict(zip(key, value)) | |
# In[7]: | |
firstsheet = True | |
for sheet_name in wb.get_sheet_names(): | |
if firstsheet == True: | |
firstsheet = False | |
continue | |
sheet = wb.get_sheet_by_name(sheet_name) | |
firstline = True | |
for cell in sheet['C']: | |
if firstline: | |
firstline = False | |
continue | |
var = cell.value.split()[1].split(".")[1].replace("\"", "") | |
sheet.cell(row=cell.row, column=4).value = var_dict[str(var)] | |
wb.save("rsquared_var.xlsx") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment