qrpa 1.0.25__py3-none-any.whl → 1.0.26__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.
Potentially problematic release.
This version of qrpa might be problematic. Click here for more details.
qrpa/fun_excel.py
CHANGED
|
@@ -1860,6 +1860,117 @@ def format_to_text_v2(sheet, columns=None):
|
|
|
1860
1860
|
log(f'设置[{col_name}] 文本格式')
|
|
1861
1861
|
sheet.range(f'{col_name}:{col_name}').number_format = '@'
|
|
1862
1862
|
|
|
1863
|
+
def format_to_text_v2_safe(sheet, columns=None, data_rows=None):
|
|
1864
|
+
"""
|
|
1865
|
+
更安全的文本格式化函数,避免COM异常
|
|
1866
|
+
|
|
1867
|
+
Args:
|
|
1868
|
+
sheet: Excel工作表对象
|
|
1869
|
+
columns: 要格式化的列名列表
|
|
1870
|
+
data_rows: 数据行数,用于限制格式化范围
|
|
1871
|
+
"""
|
|
1872
|
+
if columns is None or len(columns) == 0:
|
|
1873
|
+
return
|
|
1874
|
+
|
|
1875
|
+
# 确保columns是列表
|
|
1876
|
+
if not isinstance(columns, list):
|
|
1877
|
+
columns = [columns]
|
|
1878
|
+
|
|
1879
|
+
for col_name in columns:
|
|
1880
|
+
try:
|
|
1881
|
+
if isinstance(col_name, int):
|
|
1882
|
+
col_name = xw.utils.col_name(col_name)
|
|
1883
|
+
|
|
1884
|
+
log(f'安全设置[{col_name}] 文本格式')
|
|
1885
|
+
|
|
1886
|
+
# 如果指定了数据行数,只格式化有数据的范围
|
|
1887
|
+
if data_rows and data_rows > 0:
|
|
1888
|
+
# 格式化从第1行到数据行数的范围
|
|
1889
|
+
range_str = f'{col_name}1:{col_name}{data_rows}'
|
|
1890
|
+
sheet.range(range_str).number_format = '@'
|
|
1891
|
+
else:
|
|
1892
|
+
# 检查列是否有数据,如果没有则跳过
|
|
1893
|
+
try:
|
|
1894
|
+
# 先检查第一个单元格是否存在
|
|
1895
|
+
test_range = sheet.range(f'{col_name}1')
|
|
1896
|
+
if test_range.value is not None or sheet.used_range.last_cell.column >= column_name_to_index(col_name) + 1:
|
|
1897
|
+
sheet.range(f'{col_name}:{col_name}').number_format = '@'
|
|
1898
|
+
else:
|
|
1899
|
+
log(f'列 {col_name} 没有数据,跳过格式化')
|
|
1900
|
+
except:
|
|
1901
|
+
log(f'列 {col_name} 格式化失败,跳过')
|
|
1902
|
+
|
|
1903
|
+
except Exception as e:
|
|
1904
|
+
log(f'设置列 {col_name} 文本格式失败: {e},继续处理其他列')
|
|
1905
|
+
|
|
1906
|
+
def pre_format_columns_safe(sheet, columns, data_rows):
|
|
1907
|
+
"""
|
|
1908
|
+
预格式化函数:在写入数据前安全地设置列格式
|
|
1909
|
+
|
|
1910
|
+
Args:
|
|
1911
|
+
sheet: Excel工作表对象
|
|
1912
|
+
columns: 要格式化的列名列表
|
|
1913
|
+
data_rows: 预期数据行数
|
|
1914
|
+
"""
|
|
1915
|
+
if not columns or not isinstance(columns, list):
|
|
1916
|
+
return
|
|
1917
|
+
|
|
1918
|
+
for col_name in columns:
|
|
1919
|
+
try:
|
|
1920
|
+
if isinstance(col_name, int):
|
|
1921
|
+
col_name = xw.utils.col_name(col_name)
|
|
1922
|
+
|
|
1923
|
+
log(f'预格式化列 [{col_name}] 为文本格式')
|
|
1924
|
+
|
|
1925
|
+
# 方法1:先创建最小范围,避免整列操作
|
|
1926
|
+
try:
|
|
1927
|
+
# 创建足够大的范围来覆盖预期数据
|
|
1928
|
+
range_str = f'{col_name}1:{col_name}{max(data_rows, 1000)}'
|
|
1929
|
+
sheet.range(range_str).number_format = '@'
|
|
1930
|
+
log(f'预格式化成功: {range_str}')
|
|
1931
|
+
except Exception as e1:
|
|
1932
|
+
log(f'预格式化方法1失败: {e1}')
|
|
1933
|
+
|
|
1934
|
+
# 方法2:逐行设置格式,更安全但稍慢
|
|
1935
|
+
try:
|
|
1936
|
+
for row in range(1, data_rows + 1):
|
|
1937
|
+
cell = sheet.range(f'{col_name}{row}')
|
|
1938
|
+
cell.number_format = '@'
|
|
1939
|
+
log(f'逐行预格式化成功: {col_name}')
|
|
1940
|
+
except Exception as e2:
|
|
1941
|
+
log(f'逐行预格式化也失败: {e2}')
|
|
1942
|
+
|
|
1943
|
+
except Exception as e:
|
|
1944
|
+
log(f'预格式化列 {col_name} 失败: {e},继续处理其他列')
|
|
1945
|
+
|
|
1946
|
+
def post_format_columns_safe(sheet, columns, data_rows):
|
|
1947
|
+
"""
|
|
1948
|
+
后格式化函数:在写入数据后确认列格式
|
|
1949
|
+
|
|
1950
|
+
Args:
|
|
1951
|
+
sheet: Excel工作表对象
|
|
1952
|
+
columns: 要格式化的列名列表
|
|
1953
|
+
data_rows: 实际数据行数
|
|
1954
|
+
"""
|
|
1955
|
+
if not columns or not isinstance(columns, list):
|
|
1956
|
+
return
|
|
1957
|
+
|
|
1958
|
+
for col_name in columns:
|
|
1959
|
+
try:
|
|
1960
|
+
if isinstance(col_name, int):
|
|
1961
|
+
col_name = xw.utils.col_name(col_name)
|
|
1962
|
+
|
|
1963
|
+
log(f'后格式化列 [{col_name}] 为文本格式')
|
|
1964
|
+
|
|
1965
|
+
# 只对实际有数据的行进行格式化
|
|
1966
|
+
if data_rows > 0:
|
|
1967
|
+
range_str = f'{col_name}1:{col_name}{data_rows}'
|
|
1968
|
+
sheet.range(range_str).number_format = '@'
|
|
1969
|
+
log(f'后格式化成功: {range_str}')
|
|
1970
|
+
|
|
1971
|
+
except Exception as e:
|
|
1972
|
+
log(f'后格式化列 {col_name} 失败: {e},继续处理其他列')
|
|
1973
|
+
|
|
1863
1974
|
def format_to_text(sheet, columns=None):
|
|
1864
1975
|
if columns is None:
|
|
1865
1976
|
return
|
|
@@ -2469,12 +2580,25 @@ def batch_excel_operations(excel_path, operations):
|
|
|
2469
2580
|
data, format_to_text_colunm = args[0], args[1:] if len(args) > 1 else None
|
|
2470
2581
|
# 清空工作表
|
|
2471
2582
|
sheet.clear()
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2583
|
+
|
|
2584
|
+
# 先设置文本格式,再写入数据(确保格式生效)
|
|
2585
|
+
if format_to_text_colunm and format_to_text_colunm[0]:
|
|
2586
|
+
try:
|
|
2587
|
+
# 使用安全的预格式化方式
|
|
2588
|
+
pre_format_columns_safe(sheet, format_to_text_colunm[0], len(data))
|
|
2589
|
+
except Exception as e:
|
|
2590
|
+
log(f"预格式化失败: {e},继续执行")
|
|
2591
|
+
|
|
2475
2592
|
# 写入数据
|
|
2476
2593
|
sheet.range('A1').value = data
|
|
2477
2594
|
log(f"批量操作:写入数据到 {sheet_name}")
|
|
2595
|
+
|
|
2596
|
+
# 写入后再次确认格式(双重保险)
|
|
2597
|
+
if format_to_text_colunm and format_to_text_colunm[0]:
|
|
2598
|
+
try:
|
|
2599
|
+
post_format_columns_safe(sheet, format_to_text_colunm[0], len(data))
|
|
2600
|
+
except Exception as e:
|
|
2601
|
+
log(f"后格式化失败: {e}")
|
|
2478
2602
|
|
|
2479
2603
|
elif operation_type == 'format':
|
|
2480
2604
|
format_func, format_args = args[0], args[1:] if len(args) > 1 else ()
|
|
@@ -2,7 +2,7 @@ qrpa/RateLimitedSender.py,sha256=hqvb1qspDFaW4RsLuVufylOrefkMgixANKeBaGEqYb4,142
|
|
|
2
2
|
qrpa/__init__.py,sha256=OMQjTO0rE2XDyxsM6wVeWfI1rKFyXRGO5oPU6iLqfoA,980
|
|
3
3
|
qrpa/db_migrator.py,sha256=2VmhzcMsU0MKpl-mNCwKyV8tLTqyEysSpP27-S_rQZ8,21862
|
|
4
4
|
qrpa/fun_base.py,sha256=qg6SvR-GEj2TclB1OL9eLu711jV-bysXJ5Eh2gW9pE8,10600
|
|
5
|
-
qrpa/fun_excel.py,sha256=
|
|
5
|
+
qrpa/fun_excel.py,sha256=qGiTupcGJdZyVZL-LMxkZDTpOPhobzxZesU2Tr8iOl0,111619
|
|
6
6
|
qrpa/fun_file.py,sha256=yzjDV16WL5vRys7J4uQcNzIFkX4D5MAlSCwxcD-mwQo,11966
|
|
7
7
|
qrpa/fun_web.py,sha256=5QLQorAhRzMOGMRh4eCJ2UH8ZhVHvxkHwobWhmgU5qM,6286
|
|
8
8
|
qrpa/fun_win.py,sha256=-LnTeocdTt72NVH6VgLdpAT9_C5oV9okeudXG6CftMA,8034
|
|
@@ -17,7 +17,7 @@ qrpa/temu_lib.py,sha256=hYB59zsLS3m3NTic_duTwPMOTSxlHyQVa8OhHnHm-1g,7199
|
|
|
17
17
|
qrpa/time_utils.py,sha256=ef0hhbN_6b-gcnz5ETIVOoxemIMvcxGVGGIRnHnGaBo,29564
|
|
18
18
|
qrpa/time_utils_example.py,sha256=shHOXKKF3QSzb0SHsNc34M61wEkkLuM30U9X1THKNS8,8053
|
|
19
19
|
qrpa/wxwork.py,sha256=Vy8PGEtlTWt4-1laVhuqpJUGCFH2JymgbjvH00aaBog,10946
|
|
20
|
-
qrpa-1.0.
|
|
21
|
-
qrpa-1.0.
|
|
22
|
-
qrpa-1.0.
|
|
23
|
-
qrpa-1.0.
|
|
20
|
+
qrpa-1.0.26.dist-info/METADATA,sha256=tsIRZvpoWWACWKLq_fvsrrwY84lken14sYSQeCtzCrQ,231
|
|
21
|
+
qrpa-1.0.26.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
22
|
+
qrpa-1.0.26.dist-info/top_level.txt,sha256=F6T5igi0fhXDucPPUbmmSC0qFCDEsH5eVijfVF48OFU,5
|
|
23
|
+
qrpa-1.0.26.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|