xlwingsapi要如何导入
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xlwingsapi要如何导入相关的知识,希望对你有一定的参考价值。
xlwingsapi要如何导入起初我发现python的flask模块结合excel函数WEBSERVICE,ENCODEURL可以实现在excel中调用python实现一些功能,比如md5,sha1加密,后来发现python可以直接用xlrd/xlwt这类模块操作excel,但xlrd/xlwt连xlsx都不支持,便转到了openpyxl,openpyxl的缺点是不能在文件打开状态写入,不好调试,文件被写坏了都不知道。于是选择了xlwings这个可以“代替VB成为Excel后台”的插件,个人认为差强人意,最大的缺点是官方文档很多说的并不清楚
xlwings中文文档
插件安装:
xlwings安装使用
安装python
cmd:pip install xlwings
cmd:xlwings addin install
重启excel可以在功能区看到xlwings一栏如图
在这里插入图片描述
建议是用cmd安装,用户文件夹名必须是英文不然xlwings addin install会报错
ps:我安装插件就花了一天,就因为上面这个。xlwings addin install一直失败,百度搜索无果,(中途直接在github里下载插件,虽然插件导入成功,但导入函数会有关于pywin32报错,这是我推荐cmd命令安装的原因)查看报错文件里代码才定位问题,寻法修改为英文,不成,最后还是创建了一个英文账户(无奈~)
如果一切顺利(希望如此) 基本上按照xlwings:操作Excel,通过宏调用Pyhton(VBA调Python)[@author qcyfred]一文所述就可以调用python来操作excel啦,
记录我所遇到的坑:
RunPython ("from xw import say_hi; say_hi()")
1
1
!!!这我是在其评论区发现的(感谢@longnershot):vba中这段代码里的xw是python文件名,要多加注意
解析json的python代码
主要用到递归思想:
# -*- coding: utf-8 -*-
import copy
import itertools
import json
import urllib.request
import xlwings as xw
import json
from openpyxl.utils import get_column_letter
def pyFunc():
resp = urllib.request.urlopen(
"https://api.bilibili.com/x/v2/reply?pn=2&type=1&oid=455803935&sort=2")
pending_data = json.loads(resp.read())["data"]["replies"]
def getkey(data):
def get_tree_list(data, lst,tr_lsts):
for idx, item in enumerate(data) if isinstance(data, list) else data.items():
_lst = copy.deepcopy(lst)
_lst.append(idx)
if (isinstance(item, dict) and item.keys()) or (isinstance(item, list) and len(item) > 0):
get_tree_list(item, _lst, tr_lsts)
else:
tr_lsts.append(_lst)
lst = []
tr_lsts = []
get_tree_list(data, lst, tr_lsts)
return tr_lsts
tl = []
for i in pending_data:
keys = getkey(i)
for j in keys:
jlen = len(j)
if len(list(filter(lambda x: len(x) >= jlen and x[:jlen] == j, tl))) == 0:
for m in range(jlen):
if j[0:m] in tl:
tl.remove(j[0:m])
tl.append(j)
tl.sort()
wb = xw.Book.caller()
sht = wb.sheets[0]
def get_tree_dict(ptl, ptree, pdlen,pcol, prow):
dst =[list(g) for k,g in itertools.groupby(ptl,lambda x:x[pdlen])]
ctree = ptree["child"] =
mlen = max(list(map(lambda x: len(x), ptl)))
st=pcol
cdlen = pdlen+1
for ctl in dst:
title = ctl[0][pdlen]
ctd = ctree[title] =
cmlen = max(list(map(lambda x: len(x), ctl)))
ccol=st
nst=st+len(ctl)
crow =prow+mlen-cmlen+1
ctd["start"] = get_column_letter(ccol)
ctd["end"] = get_column_letter(nst-1)
rg = sht.range(ctd["start"]+str(prow) +
":"+ctd["end"]+str(crow-1))
rg.merge()
rg.value = title
if cmlen >cdlen:
get_tree_dict(ctl, ctd, cdlen,ccol, crow)
st +=len(ctl)
ptree =
get_tree_dict(tl, ptree, 0, 1, 1)
def insertData(cdata, _ptree, xs):
for idx, item in enumerate(cdata) if isinstance(cdata, list) else cdata.items():
_ctree=_ptree["child"][idx]
if (isinstance(item, dict) and item.keys()) or (isinstance(item, list) and len(item) > 0):
insertData(item, _ctree,xs)
else:
ys=_ctree["start"]
ye=_ctree["end"]
rg = sht.range(ys+str(xs) +
':'+ye+str(xs))
rg.merge()
rg.value = str(item)
# if isinstance(item, str):
# rg.api.Font.Color = 0x0000FF #b/g/r
# elif isinstance(item, int):
# rg.api.Font.Color = 0xFF0000
# else:
# rg.api.Font.Color = 0x00FF00
tlmlen=max(list(map(lambda x: len(x),tl)))
for i,v in enumerate(pending_data):
insertData(v, ptree,
其中pending_data即为待处理的json,是一个dict列表[,,....]
wb = xw.Book.caller() sht = wb.sheets[0] 按照个人需要修改sht 如sht=wb.active等等
修改for idx, item in enumerate(cdata) if isinstance(cdata, list) else cdata.items()以及(isinstance(item, dict) and item.keys()) or (isinstance(item, list) and len(item) > 0)可以只对dict类型解析,list则直接转str
调用range api,必须用字符串表示区间:range(“A1:D5”)有效,相对本与之等价的range((1,1),(5,4))无效,openpy.utils中的get_column_letter可以实现列数字转列字符串
# if isinstance(item, str):
# rg.api.Font.Color = 0x0000FF #b/g/r
# elif isinstance(item, int):
# rg.api.Font.Color = 0xFF0000
# else:
# rg.api.Font.Color = 0x00FF00
1
2
3
4
5
6
1
2
3
4
5
6
尾述 参考技术A 基本使用方法:新建一个excel文件,取名为xlwings_wb.xlsx,并新建一个sheet,取名为first_sht,在其A1单元格内插入字符串
webpack 错误:找不到要导入的文件或无法读取:波旁威士忌,如何解决?
【中文标题】webpack 错误:找不到要导入的文件或无法读取:波旁威士忌,如何解决?【英文标题】:webpack error: File to import not found or unreadable: bourbon , how to fix? 【发布时间】:2018-08-04 01:32:17 【问题描述】:我正在尝试将 Bourbon 包加载到我的 SCSS 文件中。我正在使用 Angular 2,这是我的 webpack 3.0 配置:
var path = require("path");
var webpack = require("webpack");
module.exports =
devServer:
contentBase: path.join(__dirname, "build"),
compress: true,
port: 9000
,
node:
fs: 'empty'
,
cache: true,
devtool: "eval", //or cheap-module-eval-source-map
entry:
app: path.join(__dirname, "client/app", "app.js")
,
output:
path: path.join(__dirname, "buildf"),
filename: "ha.js",
chunkFilename: "[name].js"
,
plugins: [
//Typically you'd have plenty of other plugins here as well
new webpack.DllReferencePlugin(
context: path.join(__dirname, "client"),
manifest: require("./build/vendor-manifest.json")
),
],
module:
loaders: [
test: /\.js?$/,
loader: "babel-loader",
include: [
path.join(__dirname, "client") //important for performance!
],
exclude: [
path.join(__dirname, "node_modules")
],
query:
cacheDirectory: true, //important for performance
plugins: ["transform-regenerator"],
presets: ["es2015", "stage-0"]
,
test: /\.(scss|sass)$/, loader: ['style-loader', 'css-loader', 'sass-loader'] ,
test: /\.html$/, loader: 'raw-loader' ,
test: /\.css$/, loader: 'css-loader'
]
;
当我运行 webpack 我得到这个错误:
错误 ./node_modules/css-loader!./node_modules/sass-loader!./client/app/app.scss 模块构建失败:@import "bourbon"; ^ 找不到或无法读取要导入的文件:波旁威士忌。父样式表:stdin 在 /Users/john/NG6-starter/client/app/app.scss(第 2 行,第 1 列)@ ./client/app/app.scss 4:14-116 @ ./client/app/app.component。 js @ ./client/app/app.js @multi (webpack)-dev-server/client?http://localhost:9000./client/app/app.js webpack: 编译失败。
为什么找不到波旁威士忌成分?这是code的链接
【问题讨论】:
【参考方案1】:2021 年 6 月更新:In version 8.0.0
the development team of sass-loader
has decided to move all SASS-related options to a sassOptions
object inside options
:
module:
rules: [
test: /\.scss$/,
use: [
loader: "style-loader"
,
loader: "css-loader"
,
loader: "sass-loader",
options:
sassOptions:
includePaths: ["node_modules/bourbon/core"],
]
]
8.0.0 之前:
您需要将options.includePaths
传递给sass-loader
:
module:
rules: [
test: /\.scss$/,
use: [
loader: "style-loader"
,
loader: "css-loader"
,
loader: "sass-loader",
options:
includePaths: ["node_modules/bourbon/core"]
]
]
【讨论】:
似乎您缺少sassOptions
对象(可能是对新版本的更改?): loader: "sass-loader", options: sassOptions: includePaths: ["absolute/path/a", "absolute/path/b"],,,
文档:github.com/webpack-contrib/sass-loader#sassoptions
感谢您的指出,似乎 sass-loader 开发人员使用 v8.0.0 更改了这一点。更新了答案。以上是关于xlwingsapi要如何导入的主要内容,如果未能解决你的问题,请参考以下文章