def get_common_row(stock_id, col_name, db_session=db_info, db=Info, p=__proc1): a = FmUtils() trans = a.load_from_json("./json/trans.json") trans = a.swap_dict(trans) name = a.load_from_json("./json/table_trans.json") name = a.swap_dict(name) exclude = ['revenue'] if col_name == "date": decor = [False, "", False] elif col_name in exclude: decor = [False, "Y", False] else: decor = a.load_from_json("./json/decor.json") decor = decor[trans[col_name]] if decor[1] == "%": decor[1] = "\\%" print(col_name) print(decor) res = db_session.query(getattr(db, col_name)).filter( db.stock_id == stock_id).order_by(desc(db.date)).all() if col_name == "date": line = "" elif col_name in exclude: line = name[col_name] res.pop() else: line = trans[col_name] # check if exists for i in res: if col_name == "date": line = line + "& " + str(i[0]) + decor[1] + " " else: line = line + "& " + str(p(i[0])) + decor[1] + " " line += "\\\\" if decor[0]: line = "\\rowcolor[gray]{0.9}" + line if decor[2] == 'dot': line = line + "\\hdashline" if decor[2] == 'solid': line = line + "\midrule" return (line)
from fm_utils import FmUtils from db_setup import Info,db_info,Concise_Table from sqlalchemy import exists from pprint import pprint import os a = FmUtils() for ii in range(2012,2018): stock_name = '大华股份' mode = 1 # -------------init ------------------- b = a.load_from_json("./"+stock_name+"/info.json") key = stock_name+str(ii)+'.pdf' v = b[key] if v['mode']!= 99: mode = v['mode'] path = os.path.join('./',stock_name,key) # dirc = './' + stock_name + '/' # pprint(dirc+key) print(path) d = a.read_and_clean(path,v['table_pages'],mode = mode) pprint(d) # ------------ init json ------------ name = a.load_from_json("./json/table_trans.json") json_loc = dirc + stock_name + str(int(v['year'])-1)+'.json' if os.path.exists(json_loc): set_last = a.load_from_json(json_loc) print("found") else: set_last = a.load_from_json("./json/reports_template.json")
import os from parse_pdf_v2 import PDFTarget from collections import OrderedDict from fm_utils import FmUtils a = FmUtils() cur_dir = "./" for file in os.listdir(cur_dir): try: if (file[-4:].lower() == '.pdf'): w = PDFTarget(cur_dir + file) info = w.generate_info() old_path = cur_dir + file new_name = info['stock_name'] + w.year + '.pdf' new_dir = cur_dir + info['stock_name'] print(new_dir, new_name) print('\n\n') if not (os.path.exists(new_dir)): os.mkdir(new_dir) os.rename(old_path, new_dir + '/' + new_name) else: os.rename(old_path, new_dir + '/' + new_name) if os.path.exists(new_dir + '/info.json'): info = a.load_from_json(new_dir + '/info.json') info[new_name] = w.generate_info() a.save_dict_to_json(new_dir + '/info.json', info, sort=True) else: info = OrderedDict() info[new_name] = w.generate_info() a.save_dict_to_json(new_dir + '/info.json', info, sort=True)
from fm_utils import FmUtils from db_setup import Info, db_info, Concise_Table from sqlalchemy import exists, and_, or_ a = FmUtils() stock_id = '002372' for ayear in range(2010, 2018): year = [str(ayear), str(ayear - 1)] rec = a.load_from_json("./json/table_trans.json") # load trans rec = dict(rec) tmp = [] for i in rec: if rec[i] == '0': tmp.append(i) for i in tmp: rec.pop(i) rec = a.swap_dict(rec) last = dict() this = dict() for i in rec.keys(): tmp = float(db_info.query(getattr(Concise_Table,i)).filter(and_(Concise_Table.date ==year[0]\ ,Concise_Table.stock_id==stock_id)).all()[0][0]) tmp1 = float(db_info.query(getattr(Concise_Table,i)).filter(and_(Concise_Table.date ==year[1]\ ,Concise_Table.stock_id==stock_id)).all()[0][0]) last[i] = tmp1 this[i] = tmp
from fm_utils import FmUtils from db_setup import Info, db_info, Concise_Table from sqlalchemy import exists from pprint import pprint a = FmUtils() stock_name = '万华化学' for ii in range(2011, 2018): b = a.load_from_json('./' + stock_name + '/info.json') key = stock_name + str(ii) + '.pdf' v = b[key] c = v['summary_pages'] path = './' + stock_name + '/' + key pages = str(c[0]) + '-' + str(c[1]) print(path) try: d = a.read_summary(path, pages) pprint(d) res = [(0, 0), (0, 0)] for i in d: if '经常性损益的净' in i or '经常性损益后的净' in i: if res[0] == (0, 0): res[0] = d[i] if '资产收益率' in i: res[1] = d[i] pprint(res) keys = [
from fm_utils import FmUtils from db_setup import Info, db_info from sqlalchemy import exists a = FmUtils() stock_id = 600298 for year in range(2013, 2014): path = "./aqjm_" + str(year) + ".json" r, e = a.get_ratios(a.load_from_json(path)) key = str(stock_id) + str(year) name = a.load_from_json("./trans.json") (res, ) = db_info.query(exists().where(Info.fm_id == key)) if not (res[0]): db_info.add(Info(stock_id=stock_id, date=year)) db_info.commit() # 是否考虑,整体 for i in r.keys(): db_info.query(Info).filter(Info.fm_id == key).update({name[i]: r[i]}) db_info.commit()
from fm_utils import FmUtils from db_setup import Info, db_info from sqlalchemy import exists, and_ a = FmUtils() stock_id = 1234 for year in range(2013, 2018): path = "./pdf/dfyh" + str(year) + "A0.pdf" d = a.read_and_clean(path, check_multi=True) r, e = a.get_ratios(d) key = str(stock_id) + str(year) name = a.load_from_json("./trans.json") (res, ) = db_info.query(exists().where(Info.fm_id == key)) if not (res[0]): raise ("Entry not existing") # 是否考虑,整体 for i in r.keys(): # db_info.query(Info).filter(Info.fm_id == key).update({name[i]: r[i]}) res = db_info.query(getattr(Info, name[i])).filter( and_(Info.date == year, Info.stock_id == stock_id)).all() print(res[0][0], r[i])
def write_tex(path, stock_id, flag_rotate=0): a = FmUtils() trans = a.load_from_json("./json/trans.json") num_rec = db_info.query(Info).filter(Info.stock_id == stock_id).count() stock_name = db_info.query( Info.stock_name).filter(Info.stock_id == stock_id).all()[0][0] row_one = get_common_row(stock_id, "date") row_two = "" tab_title = str(stock_id + " " + stock_name + " 基本信息") for i in range(1, num_rec + 1): row_two = row_two + "\cmidrule(l){" + str(i + 1) + "-" + str(i + 1) + "}" with open(path, 'w') as f: f.write("\\documentclass[a4paper,12pt]{article} \n") f.write("\\usepackage{booktabs}\n") f.write("\\usepackage{colortbl}\n") f.write("\\usepackage{tabu}\n") f.write("\\usepackage{xeCJK}\n") f.write("\\usepackage{adjustbox}\n") f.write("\\usepackage{rotating}\n") f.write("\\usepackage{fontspec} \n") f.write("\\setCJKmainfont[Scale=0.8]{SimSun} \n") f.write("\\setCJKmonofont{SimSun}\n") f.write( "\\setmainfont[Scale=0.85, Ligatures={Required,Common,Contextual,TeX}]{TeX Gyre Schola} \n" ) #--------------- f.write("\n\n\n") #--------------- f.write("%dashed line\n") f.write("\\usepackage{array}\n") f.write("\\usepackage{arydshln}\n") f.write("\\setlength\\dashlinedash{0.9pt}\n") f.write("\\setlength\\dashlinegap{1.5pt}\n") f.write("\\setlength\\arrayrulewidth{0.3pt}\n") f.write("\\begin{document}\n") if (flag_rotate): f.write("\\begin{sidewaystable}[!htbp]\n") else: f.write("\\begin{table}[!htbp]\n") f.write("\\centering\n") f.write("\\caption{" + tab_title + "}\\label{tab:aStrangeTable}\n") # if(flag_rotate): # f.write("\\begin{adjustbox}{angle=90}\n") seg = "l" + "r" * (num_rec) f.write("\\begin{tabular}{" + seg + "}\n") f.write("\\toprule\n") f.write(row_one + "\n") f.write(row_two + "\n") for i in trans: f.write(get_common_row(stock_id, trans[i]) + "\n") f.write("\\midrule\n") f.write( get_common_row(stock_id, 'revenue', db_info, Concise_Table, __proc2) + "\n") f.write("\\bottomrule\n") f.write("\\end{tabular}\n") if (flag_rotate): # f.write("\\end{adjustbox}\n") f.write("\\end{sidewaystable}\n") else: f.write("\\end{table}\n") f.write("\\end{document}\n")