Python - 使用 xlsxwriter 创建依赖下拉列表
Python - create dependent dropdown lists with xlsxwriter
我正在尝试使用包 'xlsxwriter'.
创建依赖下拉列表
例如,假设我使用 xlsxwriter 创建了一个 excel 文件,我希望第 'B' 列中的所有条目都在以下列表中:['a', 'b']。
然后我希望 'C' 列中的条目位于:
- ['a1', 'a2'] 如果 'B' 列中对应的值为 'a'
- ['b1', 'b2'] 如果 'B' 列中对应的值为 'b'
这是我尝试过的:
import numpy as np
import pandas as pd
vals = np.array([['a','a1'], ['b','b2'], ['a','a2']])
df = pd.DataFrame(vals)
n_rows = df.shape[0]
# Write to xlsx file
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
# Assign workbook and worksheet
workbook = writer.book
worksheet = writer.sheets['Sheet1']
# Creation of unlocked format
unlocked = workbook.add_format({'locked': False})
worksheet.set_column('B:C', None, unlocked)
# Creation of the dropdown menus
worksheet.data_validation('B2:B'+str(1+n_rows), {'validate' : 'list', 'source': ['a', 'b']})
for i in range(n_rows):
if df.loc[i, 0] == 'a':
worksheet.data_validation('C'+str(2+i), {'validate' : 'list', 'source': ['a1', 'a2']})
if df.loc[i, 0] == 'b':
worksheet.data_validation('C'+str(2+i), {'validate' : 'list', 'source': ['b1', 'b2']})
worksheet.protect()
# Close the workbook
workbook.close()
问题在于,每当我打开生成的 'test.xlsx' 文件并将单元格 'B2' 的值从 'a' 更改为 'b' 时,下拉菜单单元格 'C2' 的菜单保持 ['a1'、'a2'].
任何解决此问题的帮助将不胜感激。
最好的,
叮当声
您可以通过在 excel 中创建选项列表来实现,然后通过匹配第一个元素来创建列表。您可以在这里阅读更多相关信息:Excel: Dropdown list dependant on other dropdown list
D 列有 a 的选择,B 列有 b 的选择。
您的 excel 将如下所示:您可以将其移动到其他 sheet。
import numpy as np
import pandas as pd
vals = np.array([['a','a1'], ['b','b2'], ['a','a2']])
df = pd.DataFrame(vals)
n_rows = df.shape[0]
# Write to xlsx file
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
# Assign workbook and worksheet
workbook = writer.book
worksheet = writer.sheets['Sheet1']
# Creation of unlocked format
unlocked = workbook.add_format({'locked': False})
worksheet.set_column('B:C', None, unlocked)
worksheet.write('D1', 'a')
worksheet.write('D2', 'a1')
worksheet.write('D3', 'a2')
worksheet.write('E1', 'b')
worksheet.write('E2', 'b1')
worksheet.write('E3', 'b2')
# Creation of the dropdown menus
worksheet.data_validation('B2:B'+str(1+n_rows), {'validate' : 'list', 'source': ['a', 'b']})
for i in range(n_rows):
worksheet.data_validation('C'+str(2+i), {'validate' : 'list', 'source': '=INDEX($D:$E, 0, MATCH($B$'+str(2+i)+', $D:$E, 0))'})
worksheet.protect()
# Close the workbook
workbook.close()
我正在尝试使用包 'xlsxwriter'.
创建依赖下拉列表例如,假设我使用 xlsxwriter 创建了一个 excel 文件,我希望第 'B' 列中的所有条目都在以下列表中:['a', 'b']。 然后我希望 'C' 列中的条目位于:
- ['a1', 'a2'] 如果 'B' 列中对应的值为 'a'
- ['b1', 'b2'] 如果 'B' 列中对应的值为 'b'
这是我尝试过的:
import numpy as np
import pandas as pd
vals = np.array([['a','a1'], ['b','b2'], ['a','a2']])
df = pd.DataFrame(vals)
n_rows = df.shape[0]
# Write to xlsx file
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
# Assign workbook and worksheet
workbook = writer.book
worksheet = writer.sheets['Sheet1']
# Creation of unlocked format
unlocked = workbook.add_format({'locked': False})
worksheet.set_column('B:C', None, unlocked)
# Creation of the dropdown menus
worksheet.data_validation('B2:B'+str(1+n_rows), {'validate' : 'list', 'source': ['a', 'b']})
for i in range(n_rows):
if df.loc[i, 0] == 'a':
worksheet.data_validation('C'+str(2+i), {'validate' : 'list', 'source': ['a1', 'a2']})
if df.loc[i, 0] == 'b':
worksheet.data_validation('C'+str(2+i), {'validate' : 'list', 'source': ['b1', 'b2']})
worksheet.protect()
# Close the workbook
workbook.close()
问题在于,每当我打开生成的 'test.xlsx' 文件并将单元格 'B2' 的值从 'a' 更改为 'b' 时,下拉菜单单元格 'C2' 的菜单保持 ['a1'、'a2'].
任何解决此问题的帮助将不胜感激。
最好的, 叮当声
您可以通过在 excel 中创建选项列表来实现,然后通过匹配第一个元素来创建列表。您可以在这里阅读更多相关信息:Excel: Dropdown list dependant on other dropdown list
D 列有 a 的选择,B 列有 b 的选择。
您的 excel 将如下所示:您可以将其移动到其他 sheet。
import numpy as np
import pandas as pd
vals = np.array([['a','a1'], ['b','b2'], ['a','a2']])
df = pd.DataFrame(vals)
n_rows = df.shape[0]
# Write to xlsx file
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
# Assign workbook and worksheet
workbook = writer.book
worksheet = writer.sheets['Sheet1']
# Creation of unlocked format
unlocked = workbook.add_format({'locked': False})
worksheet.set_column('B:C', None, unlocked)
worksheet.write('D1', 'a')
worksheet.write('D2', 'a1')
worksheet.write('D3', 'a2')
worksheet.write('E1', 'b')
worksheet.write('E2', 'b1')
worksheet.write('E3', 'b2')
# Creation of the dropdown menus
worksheet.data_validation('B2:B'+str(1+n_rows), {'validate' : 'list', 'source': ['a', 'b']})
for i in range(n_rows):
worksheet.data_validation('C'+str(2+i), {'validate' : 'list', 'source': '=INDEX($D:$E, 0, MATCH($B$'+str(2+i)+', $D:$E, 0))'})
worksheet.protect()
# Close the workbook
workbook.close()