import json
import xlwings as xw
import docx
app = xw.App() # 新打开一个进程
app.display_alerts = False
app.screen_updating = False
path = r"路径\123.xls"
app = xw.App(visible=False, add_book=False)
wb = app.books.open(path)
sheet = wb.sheets[0]
last_cell = sheet.used_range.last_cell
last_row = last_cell.row
last_col = last_cell.column
data = []
for i in range(last_row):
dict1 = {}
list1 = []
for j in range(last_col):
str1 = chr(65+j)
str2 =str(i+1)
cell = wb.sheets["sheet1"].range(str1+str2).value
list1.append(cell)
for n in range(6):
dict1[n] =list1[n]
data.append(dict1)
wb.close()
app.quit()
def writeTojson(data):
with open('data.json', 'w') as f:
json.dump(data, f)
def readjson(ptah):
with open(ptah, 'r') as f:
data = json.load(f)
# print(data)
return data
writeTojson(data)
str=""
for i in range(len(data)):
dict2 = {}
dict2 = data[i]
for j in range(len(dict2)):
str = str + "\n" + dict2[j]
with open("test.txt","w") as f:
f.write(str) # 自带文件关闭功能,不需要再写f.close()
doc = docx.Document()
doc.add_paragraph(str)
doc.save('test.docx')
用 json.dumps 和 json.loads