re-common 10.0.39__py3-none-any.whl → 10.0.40__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- re_common/baselibrary/__init__.py +4 -4
- re_common/baselibrary/baseabs/__init__.py +6 -6
- re_common/baselibrary/baseabs/baseabs.py +26 -26
- re_common/baselibrary/database/mbuilder.py +132 -132
- re_common/baselibrary/database/moudle.py +93 -93
- re_common/baselibrary/database/msqlite3.py +194 -194
- re_common/baselibrary/database/mysql.py +169 -169
- re_common/baselibrary/database/sql_factory.py +26 -26
- re_common/baselibrary/mthread/MThreadingRun.py +486 -486
- re_common/baselibrary/mthread/MThreadingRunEvent.py +349 -349
- re_common/baselibrary/mthread/__init__.py +2 -2
- re_common/baselibrary/mthread/mythreading.py +695 -695
- re_common/baselibrary/pakge_other/socks.py +404 -404
- re_common/baselibrary/readconfig/config_factory.py +18 -18
- re_common/baselibrary/readconfig/ini_config.py +317 -317
- re_common/baselibrary/readconfig/toml_config.py +49 -49
- re_common/baselibrary/temporary/envdata.py +36 -36
- re_common/baselibrary/tools/all_requests/aiohttp_request.py +118 -118
- re_common/baselibrary/tools/all_requests/httpx_requet.py +102 -102
- re_common/baselibrary/tools/all_requests/mrequest.py +412 -412
- re_common/baselibrary/tools/all_requests/requests_request.py +81 -81
- re_common/baselibrary/tools/batch_compre/bijiao_batch.py +31 -31
- re_common/baselibrary/tools/contrast_db3.py +123 -123
- re_common/baselibrary/tools/copy_file.py +39 -39
- re_common/baselibrary/tools/db3_2_sizedb3.py +102 -102
- re_common/baselibrary/tools/foreachgz.py +39 -39
- re_common/baselibrary/tools/get_attr.py +10 -10
- re_common/baselibrary/tools/image_to_pdf.py +61 -61
- re_common/baselibrary/tools/java_code_deal.py +139 -139
- re_common/baselibrary/tools/javacode.py +79 -79
- re_common/baselibrary/tools/mdb_db3.py +48 -48
- re_common/baselibrary/tools/merge_file.py +171 -171
- re_common/baselibrary/tools/merge_gz_file.py +165 -165
- re_common/baselibrary/tools/mhdfstools/down_hdfs_files.py +42 -42
- re_common/baselibrary/tools/mhdfstools/hdfst.py +42 -42
- re_common/baselibrary/tools/mhdfstools/up_hdfs_files.py +38 -38
- re_common/baselibrary/tools/mongo_tools.py +50 -50
- re_common/baselibrary/tools/move_file.py +170 -170
- re_common/baselibrary/tools/move_mongo/mongo_table_to_file.py +63 -63
- re_common/baselibrary/tools/move_mongo/move_mongo_table.py +354 -354
- re_common/baselibrary/tools/move_mongo/use_mttf.py +18 -18
- re_common/baselibrary/tools/move_mongo/use_mv.py +93 -93
- re_common/baselibrary/tools/mpandas/mpandasreadexcel.py +125 -125
- re_common/baselibrary/tools/mpandas/pandas_visualization.py +7 -7
- re_common/baselibrary/tools/myparsel.py +104 -104
- re_common/baselibrary/tools/rename_dir_file.py +37 -37
- re_common/baselibrary/tools/sequoiadb_utils.py +398 -398
- re_common/baselibrary/tools/split_line_to_many.py +25 -25
- re_common/baselibrary/tools/stringtodicts.py +33 -33
- re_common/baselibrary/tools/workwechant_bot.py +84 -84
- re_common/baselibrary/utils/baseaiohttp.py +296 -296
- re_common/baselibrary/utils/baseaiomysql.py +87 -87
- re_common/baselibrary/utils/baseallstep.py +191 -191
- re_common/baselibrary/utils/baseavro.py +19 -19
- re_common/baselibrary/utils/baseboto3.py +291 -291
- re_common/baselibrary/utils/basecsv.py +32 -32
- re_common/baselibrary/utils/basedict.py +133 -133
- re_common/baselibrary/utils/basedir.py +241 -241
- re_common/baselibrary/utils/baseencode.py +351 -351
- re_common/baselibrary/utils/baseencoding.py +28 -28
- re_common/baselibrary/utils/baseesdsl.py +86 -86
- re_common/baselibrary/utils/baseexcel.py +264 -264
- re_common/baselibrary/utils/baseexcept.py +109 -109
- re_common/baselibrary/utils/basefile.py +654 -654
- re_common/baselibrary/utils/baseftp.py +214 -214
- re_common/baselibrary/utils/basegzip.py +60 -60
- re_common/baselibrary/utils/basehdfs.py +135 -135
- re_common/baselibrary/utils/basehttpx.py +268 -268
- re_common/baselibrary/utils/baseip.py +87 -87
- re_common/baselibrary/utils/basejson.py +2 -2
- re_common/baselibrary/utils/baselist.py +32 -32
- re_common/baselibrary/utils/basemotor.py +190 -190
- re_common/baselibrary/utils/basemssql.py +98 -98
- re_common/baselibrary/utils/baseodbc.py +113 -113
- re_common/baselibrary/utils/basepandas.py +302 -302
- re_common/baselibrary/utils/basepeewee.py +11 -11
- re_common/baselibrary/utils/basepika.py +180 -180
- re_common/baselibrary/utils/basepydash.py +143 -143
- re_common/baselibrary/utils/basepymongo.py +230 -230
- re_common/baselibrary/utils/basequeue.py +22 -22
- re_common/baselibrary/utils/baserar.py +57 -57
- re_common/baselibrary/utils/baserequest.py +279 -279
- re_common/baselibrary/utils/baseset.py +8 -8
- re_common/baselibrary/utils/basesmb.py +403 -403
- re_common/baselibrary/utils/basestring.py +382 -382
- re_common/baselibrary/utils/basetime.py +320 -320
- re_common/baselibrary/utils/baseurl.py +121 -121
- re_common/baselibrary/utils/basezip.py +57 -57
- re_common/baselibrary/utils/core/__init__.py +7 -7
- re_common/baselibrary/utils/core/bottomutils.py +18 -18
- re_common/baselibrary/utils/core/mdeprecated.py +327 -327
- re_common/baselibrary/utils/core/mlamada.py +16 -16
- re_common/baselibrary/utils/core/msginfo.py +25 -25
- re_common/baselibrary/utils/core/requests_core.py +103 -103
- re_common/baselibrary/utils/fateadm.py +429 -429
- re_common/baselibrary/utils/importfun.py +123 -123
- re_common/baselibrary/utils/mfaker.py +57 -57
- re_common/baselibrary/utils/my_abc/__init__.py +3 -3
- re_common/baselibrary/utils/my_abc/better_abc.py +32 -32
- re_common/baselibrary/utils/mylogger.py +414 -414
- re_common/baselibrary/utils/myredisclient.py +861 -861
- re_common/baselibrary/utils/pipupgrade.py +21 -21
- re_common/baselibrary/utils/ringlist.py +85 -85
- re_common/baselibrary/utils/version_compare.py +36 -36
- re_common/baselibrary/utils/ydmhttp.py +126 -126
- re_common/facade/lazy_import.py +11 -11
- re_common/facade/loggerfacade.py +25 -25
- re_common/facade/mysqlfacade.py +467 -467
- re_common/facade/now.py +31 -31
- re_common/facade/sqlite3facade.py +257 -257
- re_common/facade/use/mq_use_facade.py +83 -83
- re_common/facade/use/proxy_use_facade.py +19 -19
- re_common/libtest/base_dict_test.py +19 -19
- re_common/libtest/baseavro_test.py +13 -13
- re_common/libtest/basefile_test.py +14 -14
- re_common/libtest/basemssql_test.py +77 -77
- re_common/libtest/baseodbc_test.py +7 -7
- re_common/libtest/basepandas_test.py +38 -38
- re_common/libtest/get_attr_test/get_attr_test_settings.py +14 -14
- re_common/libtest/get_attr_test/settings.py +54 -54
- re_common/libtest/idencode_test.py +53 -53
- re_common/libtest/iniconfig_test.py +35 -35
- re_common/libtest/ip_test.py +34 -34
- re_common/libtest/merge_file_test.py +20 -20
- re_common/libtest/mfaker_test.py +8 -8
- re_common/libtest/mm3_test.py +31 -31
- re_common/libtest/mylogger_test.py +88 -88
- re_common/libtest/myparsel_test.py +27 -27
- re_common/libtest/mysql_test.py +151 -151
- re_common/libtest/pymongo_test.py +21 -21
- re_common/libtest/split_test.py +11 -11
- re_common/libtest/sqlite3_merge_test.py +5 -5
- re_common/libtest/sqlite3_test.py +34 -34
- re_common/libtest/tomlconfig_test.py +30 -30
- re_common/libtest/use_tools_test/__init__.py +2 -2
- re_common/libtest/user/__init__.py +4 -4
- re_common/studio/__init__.py +4 -4
- re_common/studio/assignment_expressions.py +36 -36
- re_common/studio/mydash/test1.py +18 -18
- re_common/studio/pydashstudio/first.py +9 -9
- re_common/studio/streamlitstudio/first_app.py +65 -65
- re_common/studio/streamlitstudio/uber_pickups.py +23 -23
- re_common/studio/test.py +18 -18
- re_common/v2/baselibrary/business_utils/BusinessStringUtil.py +219 -219
- re_common/v2/baselibrary/business_utils/baseencodeid.py +100 -100
- re_common/v2/baselibrary/business_utils/full_doi_path.py +116 -116
- re_common/v2/baselibrary/business_utils/rel_tools.py +6 -6
- re_common/v2/baselibrary/decorators/utils.py +59 -59
- re_common/v2/baselibrary/helpers/search_packge/NearestNeighbors_test.py +105 -105
- re_common/v2/baselibrary/helpers/search_packge/fit_text_match.py +253 -253
- re_common/v2/baselibrary/helpers/search_packge/scikit_learn_text_matcher.py +260 -260
- re_common/v2/baselibrary/helpers/search_packge/test.py +1 -1
- re_common/v2/baselibrary/s3object/baseboto3.py +230 -230
- re_common/v2/baselibrary/tools/WeChatRobot.py +95 -95
- re_common/v2/baselibrary/tools/ac_ahocorasick.py +75 -75
- re_common/v2/baselibrary/tools/concurrency.py +35 -35
- re_common/v2/baselibrary/tools/data_processer/base.py +53 -53
- re_common/v2/baselibrary/tools/data_processer/data_processer.py +508 -508
- re_common/v2/baselibrary/tools/data_processer/data_reader.py +187 -187
- re_common/v2/baselibrary/tools/data_processer/data_writer.py +38 -38
- re_common/v2/baselibrary/tools/dict_tools.py +44 -44
- re_common/v2/baselibrary/tools/dolphinscheduler.py +187 -187
- re_common/v2/baselibrary/tools/hdfs_base_processor.py +204 -204
- re_common/v2/baselibrary/tools/hdfs_bulk_processor.py +67 -67
- re_common/v2/baselibrary/tools/hdfs_data_processer.py +338 -338
- re_common/v2/baselibrary/tools/hdfs_line_processor.py +74 -74
- re_common/v2/baselibrary/tools/list_tools.py +69 -69
- re_common/v2/baselibrary/tools/resume_tracker.py +94 -94
- re_common/v2/baselibrary/tools/search_hash_tools.py +54 -54
- re_common/v2/baselibrary/tools/text_matcher.py +326 -326
- re_common/v2/baselibrary/tools/unionfind_tools.py +60 -60
- re_common/v2/baselibrary/utils/BusinessStringUtil.py +196 -196
- re_common/v2/baselibrary/utils/api_net_utils.py +270 -270
- re_common/v2/baselibrary/utils/author_smi.py +361 -361
- re_common/v2/baselibrary/utils/base_string_similarity.py +158 -158
- re_common/v2/baselibrary/utils/basedict.py +37 -37
- re_common/v2/baselibrary/utils/basehdfs.py +163 -163
- re_common/v2/baselibrary/utils/basepika.py +180 -180
- re_common/v2/baselibrary/utils/basetime.py +77 -77
- re_common/v2/baselibrary/utils/db.py +156 -156
- re_common/v2/baselibrary/utils/elasticsearch.py +46 -0
- re_common/v2/baselibrary/utils/json_cls.py +16 -16
- re_common/v2/baselibrary/utils/mq.py +83 -83
- re_common/v2/baselibrary/utils/n_ary_expression_tree.py +243 -243
- re_common/v2/baselibrary/utils/string_bool.py +186 -186
- re_common/v2/baselibrary/utils/string_clear.py +246 -246
- re_common/v2/baselibrary/utils/string_smi.py +18 -18
- re_common/v2/baselibrary/utils/stringutils.py +271 -271
- re_common/vip/base_step_process.py +11 -11
- re_common/vip/baseencodeid.py +90 -90
- re_common/vip/changetaskname.py +28 -28
- re_common/vip/core_var.py +24 -24
- re_common/vip/mmh3Hash.py +89 -89
- re_common/vip/proxy/allproxys.py +127 -127
- re_common/vip/proxy/allproxys_thread.py +159 -159
- re_common/vip/proxy/cnki_proxy.py +153 -153
- re_common/vip/proxy/kuaidaili.py +87 -87
- re_common/vip/proxy/proxy_all.py +113 -113
- re_common/vip/proxy/update_kuaidaili_0.py +42 -42
- re_common/vip/proxy/wanfang_proxy.py +152 -152
- re_common/vip/proxy/wp_proxy_all.py +181 -181
- re_common/vip/read_rawid_to_txt.py +91 -91
- re_common/vip/title/__init__.py +5 -5
- re_common/vip/title/transform/TransformBookTitleToZt.py +125 -125
- re_common/vip/title/transform/TransformConferenceTitleToZt.py +139 -139
- re_common/vip/title/transform/TransformCstadTitleToZt.py +195 -195
- re_common/vip/title/transform/TransformJournalTitleToZt.py +203 -203
- re_common/vip/title/transform/TransformPatentTitleToZt.py +132 -132
- re_common/vip/title/transform/TransformRegulationTitleToZt.py +114 -114
- re_common/vip/title/transform/TransformStandardTitleToZt.py +135 -135
- re_common/vip/title/transform/TransformThesisTitleToZt.py +135 -135
- re_common/vip/title/transform/__init__.py +10 -10
- {re_common-10.0.39.dist-info → re_common-10.0.40.dist-info}/LICENSE +201 -201
- {re_common-10.0.39.dist-info → re_common-10.0.40.dist-info}/METADATA +24 -16
- re_common-10.0.40.dist-info/RECORD +249 -0
- {re_common-10.0.39.dist-info → re_common-10.0.40.dist-info}/WHEEL +1 -1
- re_common-10.0.39.dist-info/RECORD +0 -248
- {re_common-10.0.39.dist-info → re_common-10.0.40.dist-info}/top_level.txt +0 -0
|
@@ -1,264 +1,264 @@
|
|
|
1
|
-
from tempfile import NamedTemporaryFile
|
|
2
|
-
|
|
3
|
-
from openpyxl import Workbook, load_workbook
|
|
4
|
-
from openpyxl.styles import Font, Alignment
|
|
5
|
-
|
|
6
|
-
"""
|
|
7
|
-
openpyxl不支持旧的.xls文件格式,请使用xlrd读取该文件,或将其转换为最新的.xlsx文件格式。
|
|
8
|
-
"""
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
class OpenpyxlUtils(object):
|
|
12
|
-
def __init__(self):
|
|
13
|
-
self.wb = Workbook() # 该参数guess_types=True 启用了类容和格式推断但为什么Workbook 没有这个参数
|
|
14
|
-
self.ws = None
|
|
15
|
-
|
|
16
|
-
def xls_to_xlsx(self, xlsPath):
|
|
17
|
-
"""
|
|
18
|
-
https://blog.csdn.net/test_sir_cao/article/details/79133617
|
|
19
|
-
mime http://www.w3school.com.cn/media/media_mimeref.asp
|
|
20
|
-
python -m pip install pypiwin32
|
|
21
|
-
|
|
22
|
-
'类 Workbooks 的 Open 方法无效
|
|
23
|
-
https://blog.csdn.net/xiaozhuzhu1/article/details/48493529
|
|
24
|
-
出现这个错误的原因是,已有的Excel文件中有无法识别的内容,手动打开的时候,会提示:Excel发现不可读取内容,只要确认打开,再保存一下,再用程序打开的时候就不会出现这个问题了。
|
|
25
|
-
有以下几种:
|
|
26
|
-
51 = xlOpenXMLWorkbook (without macro's in 2007-2016, xlsx) 保存为xlsx
|
|
27
|
-
52 = xlOpenXMLWorkbookMacroEnabled (with or without macro's in 2007-2016, xlsm)
|
|
28
|
-
保存为xlsm带宏的格式
|
|
29
|
-
50 = xlExcel12 (Excel Binary Workbook in 2007-2016 with or without macro's, xlsb)
|
|
30
|
-
以二进制保存的工作表
|
|
31
|
-
56 = xlExcel8 (97-2003 format in Excel 2007-2016, xls)
|
|
32
|
-
以xls,2003格式保存的工作表
|
|
33
|
-
:param xlsPath:
|
|
34
|
-
:return:
|
|
35
|
-
"""
|
|
36
|
-
import win32com.client
|
|
37
|
-
excel = win32com.client.gencache.EnsureDispatch('Excel.Application') # 要看MIME手册
|
|
38
|
-
wb = excel.Workbooks.Open(xlsPath)
|
|
39
|
-
wb.SaveAs(xlsPath + 'x', FileFormat=51)
|
|
40
|
-
wb.Close()
|
|
41
|
-
excel.Application.Quit()
|
|
42
|
-
|
|
43
|
-
def set_wb_only_read(self, filename, read_only=False):
|
|
44
|
-
"""
|
|
45
|
-
只读方式打开excel
|
|
46
|
-
:param filename:
|
|
47
|
-
:param read_only:
|
|
48
|
-
:return:
|
|
49
|
-
"""
|
|
50
|
-
self.wb = load_workbook(filename=filename, read_only=read_only)
|
|
51
|
-
return self.wb
|
|
52
|
-
|
|
53
|
-
def set_wb_only_write(self):
|
|
54
|
-
"""
|
|
55
|
-
按照写的方式打开一个新的wb
|
|
56
|
-
:return:
|
|
57
|
-
"""
|
|
58
|
-
self.wb = Workbook(write_only=True)
|
|
59
|
-
return self.wb
|
|
60
|
-
|
|
61
|
-
def create_ws(self, sheetname='', sheetnum=0):
|
|
62
|
-
"""
|
|
63
|
-
获取 ws sheet 对象 此函数使用_active_sheet_index属性,默认设置为0。
|
|
64
|
-
除非您修改其值,否则您将始终使用此方法获取第一个工作表。
|
|
65
|
-
:return:
|
|
66
|
-
"""
|
|
67
|
-
if sheetname:
|
|
68
|
-
self.ws = self.wb.create_sheet(sheetname, sheetnum)
|
|
69
|
-
else:
|
|
70
|
-
# 获取第一个 sheet
|
|
71
|
-
self.ws = self.wb.active
|
|
72
|
-
return self.ws
|
|
73
|
-
|
|
74
|
-
def set_ws_title(self, title):
|
|
75
|
-
"""
|
|
76
|
-
设置工作表的名字
|
|
77
|
-
:param title:
|
|
78
|
-
:return:
|
|
79
|
-
"""
|
|
80
|
-
self.ws.title = title
|
|
81
|
-
|
|
82
|
-
def get_ws(self, title):
|
|
83
|
-
"""
|
|
84
|
-
获取一个sheet 通过title
|
|
85
|
-
:param title:
|
|
86
|
-
:return:
|
|
87
|
-
"""
|
|
88
|
-
self.ws = self.wb[title]
|
|
89
|
-
return self.ws
|
|
90
|
-
|
|
91
|
-
def get_list_sheet(self):
|
|
92
|
-
"""
|
|
93
|
-
可以通过 wb.sheetnames 获取列表
|
|
94
|
-
:return:
|
|
95
|
-
"""
|
|
96
|
-
for sheet in self.wb:
|
|
97
|
-
yield sheet
|
|
98
|
-
|
|
99
|
-
def get_sheet_copy(self):
|
|
100
|
-
"""
|
|
101
|
-
得到当前sheet的一个副本
|
|
102
|
-
:return:
|
|
103
|
-
"""
|
|
104
|
-
return self.wb.copy_worksheet(self.ws)
|
|
105
|
-
|
|
106
|
-
def get_ws_value(self, key):
|
|
107
|
-
"""
|
|
108
|
-
如 A4
|
|
109
|
-
:param key:
|
|
110
|
-
:return:
|
|
111
|
-
"""
|
|
112
|
-
return self.ws[key].value
|
|
113
|
-
|
|
114
|
-
def set_ws_value(self, key, value):
|
|
115
|
-
self.ws[key] = value
|
|
116
|
-
return self.ws[key]
|
|
117
|
-
|
|
118
|
-
def get_sheet_value_cell(self, row, colum, value):
|
|
119
|
-
"""
|
|
120
|
-
使用cell方法访问指定单元格如果没有会创建
|
|
121
|
-
>>> for i in range(1,101):
|
|
122
|
-
... for j in range(1,101):
|
|
123
|
-
... ws.cell(row=i, column=j)
|
|
124
|
-
将在内存中创建100x100个单元格,无需任何操作。
|
|
125
|
-
:param row:
|
|
126
|
-
:param colum:
|
|
127
|
-
:param value:
|
|
128
|
-
:return:
|
|
129
|
-
"""
|
|
130
|
-
return self.ws.cell(row, colum, value)
|
|
131
|
-
|
|
132
|
-
def get_sheet_value_slicing(self, index1, index2=None):
|
|
133
|
-
"""
|
|
134
|
-
|
|
135
|
-
>>> colC = ws['C']
|
|
136
|
-
>>> col_range = ws['C:D']
|
|
137
|
-
>>> row10 = ws[10]
|
|
138
|
-
>>> row_range = ws[5:10]
|
|
139
|
-
|
|
140
|
-
:param index1: 数字或字母
|
|
141
|
-
:param index2:
|
|
142
|
-
:return: 通过传入的参数获取多个 tuple结果 比如 传入 A C 将返回A B C 3个tuple元组
|
|
143
|
-
也可传入数字 传入数字将返回行,传入字母将返回列 也是以元组的方式返回
|
|
144
|
-
注意 迭代元组得到 cell对象 可以 通过 cell.value得到值cell.row,cell.column得到行和列
|
|
145
|
-
"""
|
|
146
|
-
if index2 is None:
|
|
147
|
-
return self.ws[index1]
|
|
148
|
-
return self.ws[index1:index2]
|
|
149
|
-
|
|
150
|
-
def get_sheet_iter_rows(self, min_row, max_col, max_row):
|
|
151
|
-
for row in self.ws.iter_rows(min_row, max_col, max_row):
|
|
152
|
-
for cell in row:
|
|
153
|
-
yield cell
|
|
154
|
-
|
|
155
|
-
def get_sheet_iter_cols(self, min_row, max_col, max_row):
|
|
156
|
-
for row in self.ws.iter_cols(min_row, max_col, max_row):
|
|
157
|
-
for cell in row:
|
|
158
|
-
yield cell
|
|
159
|
-
|
|
160
|
-
def get_sheet_all_rows(self, is_row=True):
|
|
161
|
-
"""
|
|
162
|
-
得到所有行和列 使用 tuple()查看对象
|
|
163
|
-
:return:
|
|
164
|
-
"""
|
|
165
|
-
if is_row:
|
|
166
|
-
return self.ws.rows
|
|
167
|
-
else:
|
|
168
|
-
return self.ws.columns
|
|
169
|
-
|
|
170
|
-
def set_sheet_cell_value(self, cell, value):
|
|
171
|
-
"""
|
|
172
|
-
为单元格设置一个值
|
|
173
|
-
:param cell:
|
|
174
|
-
:param value:
|
|
175
|
-
:return:
|
|
176
|
-
"""
|
|
177
|
-
cell.value = value
|
|
178
|
-
print(cell.value)
|
|
179
|
-
|
|
180
|
-
def save_wb(self, excelname):
|
|
181
|
-
"""
|
|
182
|
-
保存为excel文件
|
|
183
|
-
:param excelname:
|
|
184
|
-
:return:
|
|
185
|
-
"""
|
|
186
|
-
self.wb.save(excelname)
|
|
187
|
-
|
|
188
|
-
def save_stream(self):
|
|
189
|
-
"""
|
|
190
|
-
保存到流
|
|
191
|
-
:return:
|
|
192
|
-
"""
|
|
193
|
-
with NamedTemporaryFile() as tmp:
|
|
194
|
-
self.wb.save(tmp.name)
|
|
195
|
-
tmp.seek(0)
|
|
196
|
-
stream = tmp.read()
|
|
197
|
-
return stream
|
|
198
|
-
|
|
199
|
-
def save_wb_template(self, excelname, templatename):
|
|
200
|
-
"""
|
|
201
|
-
>>> wb = load_workbook('document.xlsx')
|
|
202
|
-
>>> wb.template = True
|
|
203
|
-
>>> wb.save('document_template.xltx')
|
|
204
|
-
您可以指定属性template = True,以将工作簿另存为模板:
|
|
205
|
-
或者将此属性设置为False(默认),以保存为文档
|
|
206
|
-
>>> wb = load_workbook('document_template.xltx')
|
|
207
|
-
>>> wb.template = False
|
|
208
|
-
>>> wb.save('document.xlsx', as_template=False)
|
|
209
|
-
:param excelname:
|
|
210
|
-
:return:
|
|
211
|
-
"""
|
|
212
|
-
|
|
213
|
-
self.wb.template = True
|
|
214
|
-
self.wb.save(excelname)
|
|
215
|
-
|
|
216
|
-
def max_column(self):
|
|
217
|
-
"""
|
|
218
|
-
所有列
|
|
219
|
-
:return:
|
|
220
|
-
"""
|
|
221
|
-
return self.ws.max_column
|
|
222
|
-
|
|
223
|
-
def max_row(self):
|
|
224
|
-
"""
|
|
225
|
-
所有行
|
|
226
|
-
:return:
|
|
227
|
-
"""
|
|
228
|
-
return self.ws.max_row
|
|
229
|
-
|
|
230
|
-
def save_excel(self, filename):
|
|
231
|
-
"""
|
|
232
|
-
保存excel文件
|
|
233
|
-
:param filename:
|
|
234
|
-
:return:
|
|
235
|
-
"""
|
|
236
|
-
self.wb.save(filename)
|
|
237
|
-
|
|
238
|
-
def get_font(self):
|
|
239
|
-
"""
|
|
240
|
-
name 字符串 字体名称,例如'Calibri'或'Times New Roman'
|
|
241
|
-
|
|
242
|
-
size 整数 字体大小
|
|
243
|
-
|
|
244
|
-
bold 布尔 True,粗体字体
|
|
245
|
-
|
|
246
|
-
italic 布尔 True,斜体字体
|
|
247
|
-
:return:
|
|
248
|
-
"""
|
|
249
|
-
fontObj1 = Font(bold=True)
|
|
250
|
-
return fontObj1
|
|
251
|
-
|
|
252
|
-
def get_Alignment(self):
|
|
253
|
-
al = Alignment(horizontal="center")
|
|
254
|
-
return al
|
|
255
|
-
|
|
256
|
-
def set_font(self, fontobj, alignment, cells=[]):
|
|
257
|
-
"""
|
|
258
|
-
:param cell: A1
|
|
259
|
-
:param fontobj:
|
|
260
|
-
:return:
|
|
261
|
-
"""
|
|
262
|
-
for cell in cells:
|
|
263
|
-
self.ws[cell].font = fontobj
|
|
264
|
-
self.ws[cell].alignment = alignment
|
|
1
|
+
from tempfile import NamedTemporaryFile
|
|
2
|
+
|
|
3
|
+
from openpyxl import Workbook, load_workbook
|
|
4
|
+
from openpyxl.styles import Font, Alignment
|
|
5
|
+
|
|
6
|
+
"""
|
|
7
|
+
openpyxl不支持旧的.xls文件格式,请使用xlrd读取该文件,或将其转换为最新的.xlsx文件格式。
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class OpenpyxlUtils(object):
|
|
12
|
+
def __init__(self):
|
|
13
|
+
self.wb = Workbook() # 该参数guess_types=True 启用了类容和格式推断但为什么Workbook 没有这个参数
|
|
14
|
+
self.ws = None
|
|
15
|
+
|
|
16
|
+
def xls_to_xlsx(self, xlsPath):
|
|
17
|
+
"""
|
|
18
|
+
https://blog.csdn.net/test_sir_cao/article/details/79133617
|
|
19
|
+
mime http://www.w3school.com.cn/media/media_mimeref.asp
|
|
20
|
+
python -m pip install pypiwin32
|
|
21
|
+
|
|
22
|
+
'类 Workbooks 的 Open 方法无效
|
|
23
|
+
https://blog.csdn.net/xiaozhuzhu1/article/details/48493529
|
|
24
|
+
出现这个错误的原因是,已有的Excel文件中有无法识别的内容,手动打开的时候,会提示:Excel发现不可读取内容,只要确认打开,再保存一下,再用程序打开的时候就不会出现这个问题了。
|
|
25
|
+
有以下几种:
|
|
26
|
+
51 = xlOpenXMLWorkbook (without macro's in 2007-2016, xlsx) 保存为xlsx
|
|
27
|
+
52 = xlOpenXMLWorkbookMacroEnabled (with or without macro's in 2007-2016, xlsm)
|
|
28
|
+
保存为xlsm带宏的格式
|
|
29
|
+
50 = xlExcel12 (Excel Binary Workbook in 2007-2016 with or without macro's, xlsb)
|
|
30
|
+
以二进制保存的工作表
|
|
31
|
+
56 = xlExcel8 (97-2003 format in Excel 2007-2016, xls)
|
|
32
|
+
以xls,2003格式保存的工作表
|
|
33
|
+
:param xlsPath:
|
|
34
|
+
:return:
|
|
35
|
+
"""
|
|
36
|
+
import win32com.client
|
|
37
|
+
excel = win32com.client.gencache.EnsureDispatch('Excel.Application') # 要看MIME手册
|
|
38
|
+
wb = excel.Workbooks.Open(xlsPath)
|
|
39
|
+
wb.SaveAs(xlsPath + 'x', FileFormat=51)
|
|
40
|
+
wb.Close()
|
|
41
|
+
excel.Application.Quit()
|
|
42
|
+
|
|
43
|
+
def set_wb_only_read(self, filename, read_only=False):
|
|
44
|
+
"""
|
|
45
|
+
只读方式打开excel
|
|
46
|
+
:param filename:
|
|
47
|
+
:param read_only:
|
|
48
|
+
:return:
|
|
49
|
+
"""
|
|
50
|
+
self.wb = load_workbook(filename=filename, read_only=read_only)
|
|
51
|
+
return self.wb
|
|
52
|
+
|
|
53
|
+
def set_wb_only_write(self):
|
|
54
|
+
"""
|
|
55
|
+
按照写的方式打开一个新的wb
|
|
56
|
+
:return:
|
|
57
|
+
"""
|
|
58
|
+
self.wb = Workbook(write_only=True)
|
|
59
|
+
return self.wb
|
|
60
|
+
|
|
61
|
+
def create_ws(self, sheetname='', sheetnum=0):
|
|
62
|
+
"""
|
|
63
|
+
获取 ws sheet 对象 此函数使用_active_sheet_index属性,默认设置为0。
|
|
64
|
+
除非您修改其值,否则您将始终使用此方法获取第一个工作表。
|
|
65
|
+
:return:
|
|
66
|
+
"""
|
|
67
|
+
if sheetname:
|
|
68
|
+
self.ws = self.wb.create_sheet(sheetname, sheetnum)
|
|
69
|
+
else:
|
|
70
|
+
# 获取第一个 sheet
|
|
71
|
+
self.ws = self.wb.active
|
|
72
|
+
return self.ws
|
|
73
|
+
|
|
74
|
+
def set_ws_title(self, title):
|
|
75
|
+
"""
|
|
76
|
+
设置工作表的名字
|
|
77
|
+
:param title:
|
|
78
|
+
:return:
|
|
79
|
+
"""
|
|
80
|
+
self.ws.title = title
|
|
81
|
+
|
|
82
|
+
def get_ws(self, title):
|
|
83
|
+
"""
|
|
84
|
+
获取一个sheet 通过title
|
|
85
|
+
:param title:
|
|
86
|
+
:return:
|
|
87
|
+
"""
|
|
88
|
+
self.ws = self.wb[title]
|
|
89
|
+
return self.ws
|
|
90
|
+
|
|
91
|
+
def get_list_sheet(self):
|
|
92
|
+
"""
|
|
93
|
+
可以通过 wb.sheetnames 获取列表
|
|
94
|
+
:return:
|
|
95
|
+
"""
|
|
96
|
+
for sheet in self.wb:
|
|
97
|
+
yield sheet
|
|
98
|
+
|
|
99
|
+
def get_sheet_copy(self):
|
|
100
|
+
"""
|
|
101
|
+
得到当前sheet的一个副本
|
|
102
|
+
:return:
|
|
103
|
+
"""
|
|
104
|
+
return self.wb.copy_worksheet(self.ws)
|
|
105
|
+
|
|
106
|
+
def get_ws_value(self, key):
|
|
107
|
+
"""
|
|
108
|
+
如 A4
|
|
109
|
+
:param key:
|
|
110
|
+
:return:
|
|
111
|
+
"""
|
|
112
|
+
return self.ws[key].value
|
|
113
|
+
|
|
114
|
+
def set_ws_value(self, key, value):
|
|
115
|
+
self.ws[key] = value
|
|
116
|
+
return self.ws[key]
|
|
117
|
+
|
|
118
|
+
def get_sheet_value_cell(self, row, colum, value):
|
|
119
|
+
"""
|
|
120
|
+
使用cell方法访问指定单元格如果没有会创建
|
|
121
|
+
>>> for i in range(1,101):
|
|
122
|
+
... for j in range(1,101):
|
|
123
|
+
... ws.cell(row=i, column=j)
|
|
124
|
+
将在内存中创建100x100个单元格,无需任何操作。
|
|
125
|
+
:param row:
|
|
126
|
+
:param colum:
|
|
127
|
+
:param value:
|
|
128
|
+
:return:
|
|
129
|
+
"""
|
|
130
|
+
return self.ws.cell(row, colum, value)
|
|
131
|
+
|
|
132
|
+
def get_sheet_value_slicing(self, index1, index2=None):
|
|
133
|
+
"""
|
|
134
|
+
|
|
135
|
+
>>> colC = ws['C']
|
|
136
|
+
>>> col_range = ws['C:D']
|
|
137
|
+
>>> row10 = ws[10]
|
|
138
|
+
>>> row_range = ws[5:10]
|
|
139
|
+
|
|
140
|
+
:param index1: 数字或字母
|
|
141
|
+
:param index2:
|
|
142
|
+
:return: 通过传入的参数获取多个 tuple结果 比如 传入 A C 将返回A B C 3个tuple元组
|
|
143
|
+
也可传入数字 传入数字将返回行,传入字母将返回列 也是以元组的方式返回
|
|
144
|
+
注意 迭代元组得到 cell对象 可以 通过 cell.value得到值cell.row,cell.column得到行和列
|
|
145
|
+
"""
|
|
146
|
+
if index2 is None:
|
|
147
|
+
return self.ws[index1]
|
|
148
|
+
return self.ws[index1:index2]
|
|
149
|
+
|
|
150
|
+
def get_sheet_iter_rows(self, min_row, max_col, max_row):
|
|
151
|
+
for row in self.ws.iter_rows(min_row, max_col, max_row):
|
|
152
|
+
for cell in row:
|
|
153
|
+
yield cell
|
|
154
|
+
|
|
155
|
+
def get_sheet_iter_cols(self, min_row, max_col, max_row):
|
|
156
|
+
for row in self.ws.iter_cols(min_row, max_col, max_row):
|
|
157
|
+
for cell in row:
|
|
158
|
+
yield cell
|
|
159
|
+
|
|
160
|
+
def get_sheet_all_rows(self, is_row=True):
|
|
161
|
+
"""
|
|
162
|
+
得到所有行和列 使用 tuple()查看对象
|
|
163
|
+
:return:
|
|
164
|
+
"""
|
|
165
|
+
if is_row:
|
|
166
|
+
return self.ws.rows
|
|
167
|
+
else:
|
|
168
|
+
return self.ws.columns
|
|
169
|
+
|
|
170
|
+
def set_sheet_cell_value(self, cell, value):
|
|
171
|
+
"""
|
|
172
|
+
为单元格设置一个值
|
|
173
|
+
:param cell:
|
|
174
|
+
:param value:
|
|
175
|
+
:return:
|
|
176
|
+
"""
|
|
177
|
+
cell.value = value
|
|
178
|
+
print(cell.value)
|
|
179
|
+
|
|
180
|
+
def save_wb(self, excelname):
|
|
181
|
+
"""
|
|
182
|
+
保存为excel文件
|
|
183
|
+
:param excelname:
|
|
184
|
+
:return:
|
|
185
|
+
"""
|
|
186
|
+
self.wb.save(excelname)
|
|
187
|
+
|
|
188
|
+
def save_stream(self):
|
|
189
|
+
"""
|
|
190
|
+
保存到流
|
|
191
|
+
:return:
|
|
192
|
+
"""
|
|
193
|
+
with NamedTemporaryFile() as tmp:
|
|
194
|
+
self.wb.save(tmp.name)
|
|
195
|
+
tmp.seek(0)
|
|
196
|
+
stream = tmp.read()
|
|
197
|
+
return stream
|
|
198
|
+
|
|
199
|
+
def save_wb_template(self, excelname, templatename):
|
|
200
|
+
"""
|
|
201
|
+
>>> wb = load_workbook('document.xlsx')
|
|
202
|
+
>>> wb.template = True
|
|
203
|
+
>>> wb.save('document_template.xltx')
|
|
204
|
+
您可以指定属性template = True,以将工作簿另存为模板:
|
|
205
|
+
或者将此属性设置为False(默认),以保存为文档
|
|
206
|
+
>>> wb = load_workbook('document_template.xltx')
|
|
207
|
+
>>> wb.template = False
|
|
208
|
+
>>> wb.save('document.xlsx', as_template=False)
|
|
209
|
+
:param excelname:
|
|
210
|
+
:return:
|
|
211
|
+
"""
|
|
212
|
+
|
|
213
|
+
self.wb.template = True
|
|
214
|
+
self.wb.save(excelname)
|
|
215
|
+
|
|
216
|
+
def max_column(self):
|
|
217
|
+
"""
|
|
218
|
+
所有列
|
|
219
|
+
:return:
|
|
220
|
+
"""
|
|
221
|
+
return self.ws.max_column
|
|
222
|
+
|
|
223
|
+
def max_row(self):
|
|
224
|
+
"""
|
|
225
|
+
所有行
|
|
226
|
+
:return:
|
|
227
|
+
"""
|
|
228
|
+
return self.ws.max_row
|
|
229
|
+
|
|
230
|
+
def save_excel(self, filename):
|
|
231
|
+
"""
|
|
232
|
+
保存excel文件
|
|
233
|
+
:param filename:
|
|
234
|
+
:return:
|
|
235
|
+
"""
|
|
236
|
+
self.wb.save(filename)
|
|
237
|
+
|
|
238
|
+
def get_font(self):
|
|
239
|
+
"""
|
|
240
|
+
name 字符串 字体名称,例如'Calibri'或'Times New Roman'
|
|
241
|
+
|
|
242
|
+
size 整数 字体大小
|
|
243
|
+
|
|
244
|
+
bold 布尔 True,粗体字体
|
|
245
|
+
|
|
246
|
+
italic 布尔 True,斜体字体
|
|
247
|
+
:return:
|
|
248
|
+
"""
|
|
249
|
+
fontObj1 = Font(bold=True)
|
|
250
|
+
return fontObj1
|
|
251
|
+
|
|
252
|
+
def get_Alignment(self):
|
|
253
|
+
al = Alignment(horizontal="center")
|
|
254
|
+
return al
|
|
255
|
+
|
|
256
|
+
def set_font(self, fontobj, alignment, cells=[]):
|
|
257
|
+
"""
|
|
258
|
+
:param cell: A1
|
|
259
|
+
:param fontobj:
|
|
260
|
+
:return:
|
|
261
|
+
"""
|
|
262
|
+
for cell in cells:
|
|
263
|
+
self.ws[cell].font = fontobj
|
|
264
|
+
self.ws[cell].alignment = alignment
|