Skip to content

Instantly share code, notes, and snippets.

@tengpeng
Created October 27, 2016 05:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tengpeng/5646da293deee8364eb808001feb3376 to your computer and use it in GitHub Desktop.
Save tengpeng/5646da293deee8364eb808001feb3376 to your computer and use it in GitHub Desktop.
This gist looks up variables' name by openpyxl in xlsx
# 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