0x00 安装Pandas和OpenPyXL
pip install pandas
pip install openpyxl
0x01 Encoding
import os
import pandas as pd
import openpyxl
from openpyxl import load_workbook
import csv
def write_dict_to_csv(dict_data, fileName):
df = pd.DataFrame.from_dict(dict_data)
df.to_csv(fileName, index=False)
def writeCSV(dict_data, fileName):
with open(fileName, 'w', newline='') as file:
writer = csv.writer(file)
print("====================================")
msgPoints_dict = {}
msgPoints_num = 51
for key, value in dict_data.items():
print(value)
if isinstance(value, list):
for item in value:
if isinstance(item, str) and item.startswith('EP') and item is not None:
msgPoints_dict[msgPoints_num] = value
msgPoints_num = msgPoints_num + 1
print("msgPoints_dict_size: " + str(len(msgPoints_dict)))
for key, value in msgPoints_dict.items():
print(key, value, end="\n")
if isinstance(value, list) and value and isinstance(value[2], str) and value[2].startswith("EP"):
tmp_list = [str(key)]
tmp_list.extend(value)
writer.writerow(tmp_list)
pass
def main():
print("=== Read Excel ===")
fileName = './doc/data.xlsx'
dict_data = {}
if os.path.exists(fileName):
wb = load_workbook(fileName)
print(wb.active)
shell = wb.active
columns_to_print = ['B', 'F', 'O']
index = 1
for row in shell.iter_rows():
tmp_list = []
for col_idx, cell in enumerate(row, start=1):
column_letter = openpyxl.utils.get_column_letter(col_idx)
if column_letter in columns_to_print:
tmp_list.append(cell.value)
dict_data[index] = tmp_list
index = index + 1
writeCSV(dict_data, "output.csv")
pass
if __name__ == "__main__":
main()