Commit 36dd3196 authored by lzpp0221's avatar lzpp0221

Initial commit

parent 0a591668
#coding=UTF-8
__author__ = "John Lee"
from airtest.core.api import *
import json
import argparse
import time
import re
import io
import sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')
auto_setup(__file__)
def argp():
paser=argparse.ArgumentParser(prog="微信公众号信息的采集",
description="这是一个实现采集微信公众号信息的命令行脚本",
usage="程序[%(prog)s] 实现了如下功能:爬取指定基金公众号的信息,并存储在D盘",
epilog='暂无额外信息。');
paser.add_argument('-a', dest='a1', help='基金名称', type=str, required=True)
args = paser.parse_args()
return args
def caiji(jijinmingcheng):
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)
keyevent("HOME")
poco.wait_for_all(poco(text="微信"),timeout=10)
poco(text="微信").click()
sleep(10)
poco("com.tencent.mm:id/j5t").click()
sleep(4.0)
poco(text="搜索").click()
text(jijinmingcheng)
sleep(8.0)
i = 0
tab = poco("com.tencent.mm:id/kpm")
while True:
if tab[i].attr("text")==jijinmingcheng:
tab[i].click()
print(tab[i])
break
i = i+1
sleep(6)
poco.wait_for_all(poco("com.tencent.mm:id/eo"),timeout=10)
poco("com.tencent.mm:id/eo").click()
sleep(6)
year = int(time.strftime("%Y", time.localtime()))-2
str_year =str(year)
res={}
for i in range(20):
tab=poco("com.tencent.mm:id/iiq")
for j in tab.child("android.view.ViewGroup"):
values=[]
if j.offspring("com.tencent.mm:id/iks").exists():
key=j.offspring("com.tencent.mm:id/iks").attr("text")
if str_year in key:
break
if j.offspring("com.tencent.mm:id/cs").exists():
for l in j.offspring("com.tencent.mm:id/cs"):
value1=l.attr("text")
if l.sibling("com.tencent.mm:id/lzh").exists():
value2=l.sibling("com.tencent.mm:id/lzh").attr("text")
value2=value2.replace("\u2004","").replace("\u2005","").replace("\u2006","").replace("\U0001f9e7","")
#value2=re.sub(R"\\U[0-9a-fA-F]+", "", value2)
values.append([value1,value2])
if len(values)==0:
continue
res[key]=values
print(values)
if str_year in key:
break
poco.swipe([0.5,0.9],[0.5,0.2],duration=2)
sleep(2.0)
print(res)
stop_app("com.tencent.mm")
str_json=json.dumps(res,indent=4,ensure_ascii=False)
filename = r"d:\微信公众号信息列表采集-" + jijinmingcheng + ".json"
with open(filename, 'w',encoding="utf-8") as f:
f.write(str_json)
#args = argp()
#caiji(args.a1)
caiji("华夏基金")
\ No newline at end of file
{"name":"python_test","versionRange":"1.0.0","children":[],"nodeState":1}
{"id":"python_test","displayName":"python_test","moduleName":"python_test","processList":[{"processType":"/主流程.flow","methodName":"/主流程.flow","displayName":"python_test","content":[],"input":[],"output":[],"desc":"","disabled":false}]}
# Caution:
# 1. When this module runs independently as a process,
# execute the main function and assign parameters by itself
# 2. It is necessary to clarify the io parameters when defining the function.
# Uncertain parameters are not supported temporarily(*args和**kwargs)
# 3. This module can be used by the instruction of "embedding:callIsolated"
# in the visualization process
# 4. Other python modules/third-party python libraries can be called by
# [import pandas as pd]
# 5. Function parameters are immutable
# supported types in parameters/returns: float bool str tuple dict
# 6. Function that dependents on python.exe is not supported.
# i.e. multiprocessing or stdin
def main(a: float, b: float) -> float:
return a + b
if __name__ == "__main__":
# user defined test code, will not be called by Cyclone
ret = main(1, 2)
print("main(1, 2)", ret)
{
"name": "python_test",
"description": "",
"manual": "",
"outputType": "project",
"main": "/主流程.flow",
"version": "1.0.0",
"engines": {
"designer": "5.0.RC",
"server": "5.0.RC",
"executor-runtime": "4.2.0"
},
"uuid": "1e25f733-92b1-4360-a661-3e79bcfff941",
"dependencies": {}
}
\ No newline at end of file
This diff is collapsed.
# -*- coding:utf-8 -*-
import pandas as pd
import numpy as np
import json
import collections
area_data=pd.read_excel('jjbtest.xlsx')
print (area_data.head())
#res=[]
#for i in range(len(area_data)):
# area_dict=collections.OrderedDict() ##ŔűÓĂOrderedDict()˝¨Á˘ÓĐĐň´Ęľä
# area_dict["dianji"]=area_data.index[i,"column1"]
# area_dict["huifu"]=str(area_data.index[i,"column2"])
# area_dict["biaoti-1"]=str(area_data.index[i,"column3"])
# area_dict["biaoti-2"]=str(area_data.index[i,"column4"])
# area_dict["zuozhe"]=str(area_data.index[i,"column5"])
# area_dict["fabiaoriqi"]=str(area_data.index[i,"column6"])
# area_dict["gengxinriqi"]=str(area_data.index[i,"column7"])
#print (area_dict)
# -*- coding:utf-8 -*-
import pandas as pd
import numpy as np
import json
import collections
area_data=pd.read_excel('jjbtest.xlsx')
print (area_data)
res=[]
for i in range(len(area_data)):
area_dict=collections.OrderedDict() ##ŔűÓĂOrderedDict()˝¨Á˘ÓĐĐň´Ęľä
area_dict["dianji"]=area_data.index[i,"column1"]
area_dict["huifu"]=str(area_data.index[i,"column2"])
area_dict["biaoti-1"]=str(area_data.index[i,"column3"])
area_dict["biaoti-2"]=str(area_data.index[i,"column4"])
area_dict["zuozhe"]=str(area_data.index[i,"column5"])
area_dict["fabiaoriqi"]=str(area_data.index[i,"column6"])
area_dict["gengxinriqi"]=str(area_data.index[i,"column7"])
res.append(area_dict)
print (res)
This source diff could not be displayed because it is too large. You can view the blob instead.
import openpyxl
import json
import io
import re
import datetime
# excel表格转json文件
def excel_to_json(excel_file, json_file_name):
# 加载工作薄
book = openpyxl.load_workbook(excel_file)
# 获取sheet页
sheet = book["Sheet1"]
# 行数
max_row = sheet.max_row
# 列数
max_column = sheet.max_column
print("max_row: %d, max_column: %d" % (max_row, max_column))
# 结果,数组存储
result = []
heads = []
# 解析表头
for column in range(max_column):
# 读取的话行列是从(1,1)开始
heads.append(sheet.cell(1, column + 1).value)
# 遍历每一行
for row in range(max_row):
if row == 0:
continue
one_line = {}
for column in range(max_column):
# 读取第二行开始每一个数据
k = heads[column]
cell = sheet.cell(row + 1, column + 1)
value = cell.value
one_line[k] = value
print(one_line)
result.append(one_line)
book.close()
# 将json保存为文件
save_json_file(result, json_file_name)
# 将json保存为文件
def save_json_file(jd, json_file_name):
file = io.open(json_file_name, 'w', encoding='utf-8')
# 把对象转化为json对象
# indent: 参数根据数据格式缩进显示,读起来更加清晰
# ensure_ascii = True:默认输出ASCII码,如果把这个该成False, 就可以输出中文。
txt = json.dumps(jd, indent=2, ensure_ascii=False)
file.write(txt)
file.close()
def json_year():
today = datetime.datetime.today()
year = today.year
with open(r"d:\jjb.json", 'r',encoding='utf-8', errors='ignore') as f:
str_json=f.read()
res = json.loads(str_json)
key7 = list(res[0].keys())[-1]
#print(res)
for i,ele in enumerate(res):
gengxinriqi=ele[key7]
if i==0:
gengxinriqi_1=""
else:
gengxinriqi_1=res[i-1][key7]
if ("01-01" in gengxinriqi) and "01-01" not in gengxinriqi_1:
year=year-1
res[i][key7] = str(year)+" "+gengxinriqi
str_json=json.dumps(res,indent=4,ensure_ascii=False)
with open(r"d:\jjb_g.json", 'w',encoding="utf-8") as f:
f.write(str_json)
#if '__main__' == __name__:
# excel_to_json(u'd:\jjb.xlsx', 'd:\jjb.json')
# json_year()
import xlrd
res = []
wb = xlrd.open_workbook("jjbtest.xls")
sh = wb.sheet_by_index(0)
title = []
for item in sh.row_values(0):
title.append(item)
data = []
# 实现第一行为key,剩下的为value 转为字典了
[[data.append({title[index]: sh.row_values(it)[index] for index in range(0,len(title))})] for it in range(1,sh.nrows)]
print(data)
\ No newline at end of file
......@@ -4,7 +4,7 @@
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>768a8cb1-0fbc-4320-89e7-cc13c6fd2333</ProjectGuid>
<ProjectHome>.</ProjectHome>
<StartupFile>xlstojson.py</StartupFile>
<StartupFile>x-j-openpyxl.py</StartupFile>
<SearchPath>
</SearchPath>
<WorkingDirectory>.</WorkingDirectory>
......@@ -21,8 +21,22 @@
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
</PropertyGroup>
<ItemGroup>
<Compile Include="module1.py">
<SubType>Code</SubType>
</Compile>
<Compile Include="x-j-openpyxl.py">
<SubType>Code</SubType>
</Compile>
<Compile Include="module3.py">
<SubType>Code</SubType>
</Compile>
<Compile Include="xlstojson.py" />
</ItemGroup>
<ItemGroup>
<Content Include="jjbtest.csv" />
<Content Include="jjbtest.xls" />
<Content Include="jjbtest.xlsx" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.targets" />
<!-- Uncomment the CoreCompile target to enable the Build command in
Visual Studio and specify your pre- and post-build commands in
......
......@@ -5,6 +5,10 @@ from airtest.core.api import *
import json
import argparse
import time
import re
import io
import sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')
auto_setup(__file__)
def argp():
......@@ -22,23 +26,24 @@ def caiji(jijinmingcheng):
keyevent("HOME")
poco.wait_for_all(poco(text="微信"),timeout=10)
poco(text="微信").click()
sleep(4)
sleep(10)
poco("com.tencent.mm:id/j5t").click()
sleep(2.0)
text(jijinmingcheng)
sleep(4.0)
poco(text="搜索").click()
text(jijinmingcheng)
sleep(8.0)
i = 0
tab = poco("com.tencent.mm:id/kpm")
while True:
if tab[i].attr("text")==jijinmingcheng:
tab[i].click()
print(tab[i])
#print(tab[i])
break
i = i+1
sleep(4)
sleep(6)
poco.wait_for_all(poco("com.tencent.mm:id/eo"),timeout=10)
poco("com.tencent.mm:id/eo").click()
sleep(4)
sleep(6)
year = int(time.strftime("%Y", time.localtime()))-2
str_year =str(year)
res={}
......@@ -55,17 +60,18 @@ def caiji(jijinmingcheng):
value1=l.attr("text")
if l.sibling("com.tencent.mm:id/lzh").exists():
value2=l.sibling("com.tencent.mm:id/lzh").attr("text")
value2=value2.replace("\u2004","").replace("\u2005","").replace("\u2006","")
value2=value2.replace("\u2004","").replace("\u2005","").replace("\u2006","").replace("\U0001f9e7","")
#value2=re.sub(R"\\U[0-9a-fA-F]+", "", value2)
values.append([value1,value2])
if len(values)==0:
continue
res[key]=values
print(values)
#print(values)
if str_year in key:
break
poco.swipe([0.5,0.9],[0.5,0.2],duration=2)
sleep(2.0)
print(res)
#print(res)
stop_app("com.tencent.mm")
str_json=json.dumps(res,indent=4,ensure_ascii=False)
filename = r"d:\微信公众号信息列表采集-" + jijinmingcheng + ".json"
......
......@@ -24,3 +24,5 @@ def json_year():
str_json=json.dumps(res,indent=4,ensure_ascii=False)
with open(r"d:\jjb_g.json", 'w',encoding="utf-8") as f:
f.write(str_json)
if '__main__' == __name__:
json_year()
\ No newline at end of file
[{"name":"ScreenResolution","type":"screen","data":{"width":1920,"height":1080}},{"name":"ScreenScale","type":"screen","data":{"scale":100}},{"name":"Chrome","type":"app","data":{"state":"install","version":"108.0.5359.125","path":"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"}},{"name":"IE","type":"app","data":{"state":"install","version":"11.0.19041.1566","path":"C:\\Program Files\\Internet Explorer\\iexplore.exe"}},{"name":"Edge","type":"app","data":{"state":"install","version":"107.0.1418.62","path":"C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe"}},{"name":"Chrome","type":"extension","data":{"state":"enable"}},{"name":"IE","type":"extension","data":{"state":"disable"}},{"name":"Edge","type":"extension","data":{"state":"disable"}},{"name":"Java","type":"extension","data":{"state":"uninstall"}},{"name":"WinIO","type":"driver","data":{"state":"uninstall"}},{"name":"HID","type":"driver","data":{"state":"uninstall"}},{"name":"WPSWord","type":"app","data":{"state":"uninstall"}},{"name":"WPSExcel","type":"app","data":{"state":"uninstall"}},{"name":"WPSPPT","type":"app","data":{"state":"uninstall"}},{"name":"MSWord","type":"app","data":{"state":"install","version":"16.0.15831.20208","path":"C:\\Program Files (x86)\\Microsoft Office\\Root\\Office16\\WINWORD.EXE","active":""}},{"name":"MSExcel","type":"app","data":{"state":"install","version":"16.0.15831.20208","path":"C:\\Program Files (x86)\\Microsoft Office\\Root\\Office16\\EXCEL.EXE","active":""}},{"name":"MSPPT","type":"app","data":{"state":"install","version":"16.0.15831.20208","path":"C:\\Program Files (x86)\\Microsoft Office\\Root\\Office16\\POWERPNT.EXE","active":""}}]
[{"name":"ScreenResolution","type":"screen","data":{"width":2736,"height":1824}},{"name":"ScreenScale","type":"screen","data":{"scale":200}},{"name":"Chrome","type":"app","data":{"state":"install","version":"108.0.5359.125","path":"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"}},{"name":"IE","type":"app","data":{"state":"install","version":"11.0.19041.1566","path":"C:\\Program Files\\Internet Explorer\\iexplore.exe"}},{"name":"Edge","type":"app","data":{"state":"install","version":"107.0.1418.62","path":"C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe"}},{"name":"Chrome","type":"extension","data":{"state":"enable"}},{"name":"IE","type":"extension","data":{"state":"disable"}},{"name":"Edge","type":"extension","data":{"state":"uninstall"}},{"name":"Java","type":"extension","data":{"state":"enable"}},{"name":"WinIO","type":"driver","data":{"state":"uninstall"}},{"name":"HID","type":"driver","data":{"state":"uninstall"}},{"name":"WPSWord","type":"app","data":{"state":"uninstall"}},{"name":"WPSExcel","type":"app","data":{"state":"uninstall"}},{"name":"WPSPPT","type":"app","data":{"state":"uninstall"}},{"name":"MSWord","type":"app","data":{"state":"install","version":"16.0.15928.20216","path":"C:\\Program Files (x86)\\Microsoft Office\\Root\\Office16\\WINWORD.EXE","active":""}},{"name":"MSExcel","type":"app","data":{"state":"install","version":"16.0.15928.20216","path":"C:\\Program Files (x86)\\Microsoft Office\\Root\\Office16\\EXCEL.EXE","active":""}},{"name":"MSPPT","type":"app","data":{"state":"install","version":"16.0.15928.20216","path":"C:\\Program Files (x86)\\Microsoft Office\\Root\\Office16\\POWERPNT.EXE","active":""}}]
{"dependencies":{"array_op":"1.0.0","datatable_op":"1.0.0","date_time":"1.0.0","file_manage":"1.0.0","flags":"1.0.0","new_excel":"1.0.0","object_op":"1.0.0","string_op":"1.0.0","struct_op":"1.0.0","system_function":"1.0.0","ui_automation":"1.0.0","var_op":"1.0.0"},"flowCnt":1,"codeCnt":20,"aiSkills":[],"flowInfos":{"/主流程.flow":{"0":{"lineNumber":0,"description":"","schemaBodyList":[{"bodyId":"body","codeList":[2,3,5,4,7,46,45,44,35,34,9]}]},"2":{"lineNumber":2,"description":"打开浏览器"},"3":{"lineNumber":3,"description":"抓取结构化数据"},"4":{"lineNumber":4,"description":"写入范围数据"},"5":{"lineNumber":5,"description":"打开Excel工作簿"},"7":{"lineNumber":7,"description":"保存Excel工作簿"},"9":{"lineNumber":9,"description":"上传文件"},"34":{"lineNumber":34,"description":"保存Excel工作簿"},"35":{"lineNumber":35,"description":"按行遍历","schemaBodyList":[{"bodyId":"body","codeList":[36,37,41,42]}]},"36":{"lineNumber":36,"description":"查找字符串"},"37":{"lineNumber":37,"description":"If条件判断","schemaBodyList":[{"bodyId":"caseBody","codeList":[38]},{"bodyId":"defaultBody","codeList":[40]}]},"38":{"lineNumber":38,"description":"如果","schemaBodyList":[{"bodyId":"trueBody","codeList":[39]}]},"39":{"lineNumber":39,"description":"变量赋值"},"40":{"lineNumber":40,"description":"否则","schemaBodyList":[{"bodyId":"body","codeList":[]}]},"41":{"lineNumber":41,"description":"变量赋值"},"42":{"lineNumber":42,"description":"写入单元格数据"},"44":{"lineNumber":44,"description":"打开Excel工作簿"},"45":{"lineNumber":45,"description":"获取日期属性"},"46":{"lineNumber":46,"description":"获取当前日期&时间"}}},"flowCalls":{"/主流程.flow":{}},"input_param":[],"output_param":[],"top_level_process":[{"description":"打开浏览器","line_number":2,"child_line_number":[]},{"description":"抓取结构化数据","line_number":3,"child_line_number":[]},{"description":"打开Excel工作簿","line_number":5,"child_line_number":[]},{"description":"写入范围数据","line_number":4,"child_line_number":[]},{"description":"保存Excel工作簿","line_number":7,"child_line_number":[]},{"description":"获取当前日期&时间","line_number":46,"child_line_number":[]},{"description":"获取日期属性","line_number":45,"child_line_number":[]},{"description":"打开Excel工作簿","line_number":44,"child_line_number":[]},{"description":"按行遍历","line_number":35,"child_line_number":[36,37,38,39,40,41,42]},{"description":"保存Excel工作簿","line_number":34,"child_line_number":[]},{"description":"上传文件","line_number":9,"child_line_number":[]}],"resources":[],"user_defined_structs":{}}
{"dependencies":{"array_op":"1.0.0","datatable_op":"1.0.0","embedding":"1.0.0","flags":"1.0.0","new_excel":"1.0.0","system_function":"1.0.0","var_op":"1.0.0","file_manage":"1.0.0","object_op":"1.0.0","ui_automation":"1.0.0"},"flowCnt":2,"codeCnt":12,"aiSkills":[],"flowInfos":{"/主流程.flow":{"0":{"lineNumber":0,"description":"","schemaBodyList":[{"bodyId":"body","codeList":[2,3,5,4,7,47,9]}]},"2":{"lineNumber":2,"description":"打开浏览器"},"3":{"lineNumber":3,"description":"抓取结构化数据"},"4":{"lineNumber":4,"description":"写入范围数据"},"5":{"lineNumber":5,"description":"打开Excel工作簿"},"7":{"lineNumber":7,"description":"保存Excel工作簿"},"9":{"lineNumber":9,"description":"上传文件"},"47":{"lineNumber":47,"description":"执行流程"}},"/python加年份.flow":{"0":{"lineNumber":0,"description":"","schemaBodyList":[{"bodyId":"body","codeList":[108,109]}]},"108":{"lineNumber":108,"description":"调用代码块"},"109":{"lineNumber":109,"description":"输出到控制台与日志"}}},"flowCalls":{"/主流程.flow":{"47":"/python加年份.flow"},"/python加年份.flow":{}},"input_param":[],"output_param":[],"top_level_process":[{"description":"打开浏览器","line_number":2,"child_line_number":[]},{"description":"抓取结构化数据","line_number":3,"child_line_number":[]},{"description":"打开Excel工作簿","line_number":5,"child_line_number":[]},{"description":"写入范围数据","line_number":4,"child_line_number":[]},{"description":"保存Excel工作簿","line_number":7,"child_line_number":[]},{"description":"执行流程","line_number":47,"child_line_number":[]},{"description":"上传文件","line_number":9,"child_line_number":[]}],"resources":[],"user_defined_structs":{}}
#coding=UTF-8
__author__ = "John Lee"
import openpyxl
import json
import io
import re
import datetime
def json_year():
today = datetime.datetime.today()
year = today.year
print(year)
with open(r"d:\jjb.json",'r',encoding='utf-8') as f:
res = json.load(f)
print(res)
key7 = list(res[0].keys())[-1]
print(key7)
for i,ele in enumerate(res):
gengxinriqi=ele[key7]
if i==0:
gengxinriqi_1=""
else:
gengxinriqi_1=res[i-1][key7]
if ("01-01" in gengxinriqi) and ("01-01" not in gengxinriqi_1):
year=year-1
res[i][key7] = str(year)+" "+gengxinriqi
print(res)
with open(r"d:\jjb_g.json",'w',encoding="utf-8") as f:
json.dump(res, f,indent=4,ensure_ascii=False) #save_json_file(result, json_file_name)
json_year()
# Caution:
# 1. When this module runs independently as a process,
# execute the main function and assign parameters by itself
# 2. It is necessary to clarify the io parameters when defining the function.
# Uncertain parameters are not supported temporarily(*args和**kwargs)
# 3. This module can be used by the instruction of "embedding:callIsolated"
# in the visualization process
# 4. Other python modules/third-party python libraries can be called by
# [import pandas as pd]
# 5. Function parameters are immutable
# supported types in parameters/returns: float bool str tuple dict
# 6. Function that dependents on python.exe is not supported.
# i.e. multiprocessing or stdin
def main(a: float, b: float) -> float:
return a + b
if __name__ == "__main__":
# user defined test code, will not be called by Cyclone
ret = main(1, 2)
print("main(1, 2)", ret)
{"name":"天天基金吧总版采集","description":"","manual":"","outputType":"project","main":"/主流程.flow","version":"1.0.2","engines":{"designer":"5.0.RC","server":"5.0.RC","executor-runtime":"4.2.0"},"uuid":"c771a848-ef8b-4ece-880c-5416e0fdbb72","dependencies":{},"comments":"2.0"}
{"name":"天天基金吧总版采集","description":"","manual":"","outputType":"project","main":"/主流程.flow","version":"1.0.4","engines":{"designer":"5.0.RC","server":"5.0.RC","executor-runtime":"4.2.0"},"uuid":"c771a848-ef8b-4ece-880c-5416e0fdbb72","dependencies":{},"comments":"修订输出"}
#coding=UTF-8
__author__ = "John Lee"
import openpyxl
import json
import io
import re
import datetime
# excel表格转json文件
def excel_to_json(excel_file, json_file_name):
# 加载工作薄
book = openpyxl.load_workbook(excel_file)
# 获取sheet页
sheet = book["Sheet1"]
# 行数
max_row = sheet.max_row
# 列数
max_column = sheet.max_column
print("max_row: %d, max_column: %d" % (max_row, max_column))
# 结果,数组存储
result = []
heads = []
# 解析表头
for column in range(max_column):
# 读取的话行列是从(1,1)开始
heads.append(sheet.cell(1, column + 1).value)
# 遍历每一行
for row in range(max_row):
if row == 0:
continue
one_line = {}
for column in range(max_column):
# 读取第二行开始每一个数据
k = heads[column]
cell = sheet.cell(row + 1, column + 1)
value = cell.value
one_line[k] = value
print(one_line)
result.append(one_line)
book.close()
# 将json保存为文件
save_json_file(result, json_file_name)
# 将json保存为文件
def save_json_file(jd, json_file_name):
file = io.open(json_file_name, 'w', encoding='utf-8')
# 把对象转化为json对象
# indent: 参数根据数据格式缩进显示,读起来更加清晰
# ensure_ascii = True:默认输出ASCII码,如果把这个该成False, 就可以输出中文。
txt = json.dumps(jd, indent=2, ensure_ascii=False)
file.write(txt)
file.close()
def json_year():
today = datetime.datetime.today()
year = today.year
with open(r"d:\jjb.json", 'r',encoding='utf-8', errors='ignore') as f:
str_json=f.read()
res = json.loads(str_json)
key7 = list(res[0].keys())[-1]
#print(res)
for i,ele in enumerate(res):
gengxinriqi=ele[key7]
if i==0:
gengxinriqi_1=""
else:
gengxinriqi_1=res[i-1][key7]
if ("01-01" in gengxinriqi) and "01-01" not in gengxinriqi_1:
year=year-1
res[i][key7] = str(year)+" "+gengxinriqi
str_json=json.dumps(res,indent=4,ensure_ascii=False)
with open(r"d:\jjb_g.json", 'w',encoding="utf-8") as f:
f.write(str_json)
#if '__main__' == __name__:
excel_to_json(u'd:\jjb.xlsx', 'd:\jjb.json')
json_year()
#coding=UTF-8
__author__ = "John Lee"
import openpyxl
import json
import io
import re
import datetime
# excel表格转json文件
def excel_to_json(excel_file: str, json_file_name: str):
# 加载工作薄
book = openpyxl.load_workbook(excel_file)
# 获取sheet页
sheet = book["Sheet1"]
# 行数
max_row = sheet.max_row
# 列数
max_column = sheet.max_column
print("max_row: %d, max_column: %d" % (max_row, max_column))
# 结果,数组存储
result = []
heads = []
# 解析表头
for column in range(max_column):
# 读取的话行列是从(1,1)开始
heads.append(sheet.cell(1, column + 1).value)
# 遍历每一行
for row in range(max_row):
if row == 0:
continue
one_line = {}
for column in range(max_column):
# 读取第二行开始每一个数据
k = heads[column]
cell = sheet.cell(row + 1, column + 1)
value = cell.value
one_line[k] = value
#print(one_line)
result.append(one_line)
book.close()
# 将json保存为文件
with open(r"d:\jjb.json", 'w',encoding="utf-8") as f:
json.dump(result, f,indent=4,ensure_ascii=False)
#save_json_file(result, json_file_name)
today = datetime.datetime.today()
year = today.year
print(year)
with open(r"d:\jjb.json", 'r',encoding='utf-8') as f:
res = json.load(f)
print(res)
key7 = list(res[0].keys())[-1]
print(key7)
for i,ele in enumerate(res):
gengxinriqi=ele[key7]
if i==0:
gengxinriqi_1=""
else:
gengxinriqi_1=res[i-1][key7]
if ("01-01" in gengxinriqi) and ("01-01" not in gengxinriqi_1):
year=year-1
res[i][key7] = str(year)+" "+gengxinriqi
print(res)
with open(r"d:\jjb_g.json", 'w',encoding="utf-8") as f:
json.dump(res, f,indent=4,ensure_ascii=False) #save_json_file(result, json_file_name)
#def json_year():
# today = datetime.datetime.today()
# year = today.year
# with open(r"d:\jjb.json", 'r',encoding='utf-8', errors='ignore') as f:
# str_json=f.read()
# res = json.loads(str_json)
# key7 = list(res[0].keys())[-1]
# #print(res)
# for i,ele in enumerate(res):
# gengxinriqi=ele[key7]
# if i==0:
# gengxinriqi_1=""
# else:
# gengxinriqi_1=res[i-1][key7]
# if ("01-01" in gengxinriqi) and "01-01" not in gengxinriqi_1:
# year=year-1
# res[i][key7] = str(year)+" "+gengxinriqi
# str_json=json.dumps(res,indent=4,ensure_ascii=False)
# with open(r"d:\jjb_g.json", 'w',encoding="utf-8") as f:
# f.write(str_json)
#if '__main__' == __name__:
def main():
excel_to_json(u'd:\jjb.xlsx', 'd:\jjb.json')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment