1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# 读取csv提供了许多使用的选项,比如可以指定分隔符,读取部分数据,跳过表头,指定列名读取
# 参考:https://www.gairuo.com/p/pandas-read-csv
pd.read_csv(
filepath_or_buffer: 'FilePathOrBuffer',
sep=<no_default>,
delimiter=None,
header='infer',
names=<no_default>,
index_col=None,
usecols=None,
squeeze=False,
prefix=<no_default>,
mangle_dupe_cols=True,
dtype: 'DtypeArg | None' = None,
engine=None,
converters=None,
true_values=None,
false_values=None,
skipinitialspace=False,
skiprows=None,
skipfooter=0,
nrows=None,
na_values=None,
keep_default_na=True,
na_filter=True,
verbose=False,
skip_blank_lines=True,
parse_dates=False,
infer_datetime_format=False,
keep_date_col=False,
date_parser=None,
dayfirst=False,
cache_dates=True,
iterator=False,
chunksize=None,
compression='infer',
thousands=None,
decimal: 'str' = '.',
lineterminator=None,
quotechar='"',
quoting=0,
doublequote=True,
escapechar=None,
comment=None,
encoding=None,
encoding_errors: 'str | None' = 'strict',
dialect=None,
error_bad_lines=None,
warn_bad_lines=None,
on_bad_lines=None,
delim_whitespace=False,
low_memory=True,
memory_map=False,
float_precision=None,
storage_options: 'StorageOptions' = None,
)
# 读取excel
# 参考https://www.gairuo.com/p/pandas-read-excel
pd.read_excel(io, sheet_name=0, header=0,
names=None, index_col=None,
usecols=None, squeeze=False,
dtype=None, engine=None,
converters=None, true_values=None,
false_values=None, skiprows=None,
nrows=None, na_values=None,
keep_default_na=True, verbose=False,
parse_dates=False, date_parser=None,
thousands=None, comment=None, skipfooter=0,
convert_float=True, mangle_dupe_cols=True, **kwargs)
# 有时候需要在已有excel中追加数据,这个在csv中很好实现设置moda='a'即可,在excel就有点麻烦了。
# 因为excel中格式比较复杂,如果表头想要合并单元格,设置颜色等样式时,我们可以先用excel设计好文件模板,然后再将数据追加到excel保存即可
# 实现如下:
import pandas as pd
from openpyxl import load_workbook
def append_excel(filepath: str):
filepath = ''
with pd.ExcelWriter(filepath, engine='openpyxl', mode='a', if_sheet_exists="overlay") as writer:
df_origin = pd.DataFrame(
pd.read_excel(filepath, sheet_name='sheet_name')) #读取原数据文件和表
book = load_workbook(filepath)
writer.book = book
# 复制原有excel所有sheet
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
#获取原数据的行数
df_rows = df_origin.shape[0]
df.to_excel(writer,
sheet_name=writer.book.active.title,
startrow=df_rows + 1,
index=False,
header=False) #将数据写入excel中的aa表,从第一个空行开始写
writer.save() #保存
|