gpt4 book ai didi

python - 使用 openpyxl 的数据验证不写入文件 - 包含代码

转载 作者:行者123 更新时间:2023-11-28 22:30:22 25 4
gpt4 key购买 nike

实际写入每个文件的代码运行良好。我遇到的问题是数据验证部分似乎没有做任何事情。在我引用的范围内没有创建下拉菜单。

在此先感谢您提供的所有帮助!

%%time

import pandas as pd
import xlsxwriter as ew
import csv as csv
import os
import glob
import openpyxl

#remove existing files from directory
files = glob.glob(#filename)
for f in files:
os.remove(f)

pendpath = #filename
df = pd.read_sas(pendpath)

allusers = df.UserID_NB.unique()
listuserpath = #filename
listusers = pd.read_csv(listuserpath)

listusers = listusers['USER_ID'].apply(lambda x: str(x).strip())

for id in listusers:
x = df.loc[df['UserID_NB']==id]
path = #filename
x.to_excel(path, sheet_name = str(id), index = False)

from openpyxl import load_workbook

wb = openpyxl.load_workbook(filename = path)
sheet = wb.get_sheet_by_name(str(id))
maxrow = sheet.max_row

from openpyxl.worksheet.datavalidation import DataValidation

dv = DataValidation(type="list", formula1='"Yes,No"', allow_blank=False, showDropDown = True)
rangevar = 'R1:T'+ str(maxrow)
dv.ranges.append(rangevar)
wb.save(path)
print str(id), rangevar

基本表代码

 import openpyxl 

wb = openpyxl.Workbook()
ws = wb.active
sheet.title = 'testsheet'

path = '#filepath'
from openpyxl.worksheet.datavalidation import DataValidation

dv = DataValidation(type="list", formula1='"Yes,No"', allow_blank=False, showDropDown = True)
dv.ranges.append('A1')
wb.save(path)

最佳答案

您缺少将 dv 添加到 worksheet

>>> # Add the data-validation object to the worksheet
>>> ws.add_data_validation(dv)

Read the docs about validation

关于python - 使用 openpyxl 的数据验证不写入文件 - 包含代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42302082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com