吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2705|回复: 4
收起左侧

[Python 转载] python版excel题库转文本txt和word

[复制链接]
ghostlg 发表于 2021-9-22 17:31
本帖最后由 ghostlg 于 2021-9-22 18:16 编辑

之前易语言版本   https://www.52pojie.cn/thread-1472488-1-1.html
之前用易语言写的工具,最近学习python又写了个python的excel转word或txt和json
import json
import xlwings as xw
import docx
app = xw.App()  # 新打开一个进程

# 禁用提示和屏幕刷新可以提升速度,操作完成后记得设置为True
app.display_alerts = False
app.screen_updating = False

#路径  前边加r表示绝对路径
path = r"自己的excel.xls"
# #打开已有excel

app = xw.App(visible=False, add_book=False)
wb = app.books.open(path)
sheet = wb.sheets[0]
# 通过last_cell方式获取最下边且最右边的一个单元格。
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()

# 写入 JSON 数据
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)
data = readjson(r"自己的.json")

# 写入txt
str=""
for i in range(len(data)):
    dict2 = {}
    dict2 = data
    for key in dict2:
        str = str + "\n" + dict2[key]

with open("test.txt","w") as f:
    f.write(str)  
# 写入word
doc = docx.Document()
doc.add_paragraph(str)

doc.save('test.docx')

免费评分

参与人数 3吾爱币 +7 热心值 +2 收起 理由
苏紫方璇 + 5 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
为妳 + 1 已替楼主添加代码,方便复制使用
马铃薯君 + 1 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

为妳 发表于 2021-9-22 21:47
[Python] 纯文本查看 复制代码
import json
import xlwings as xw
import docx
app = xw.App()  # 新打开一个进程

# 禁用提示和屏幕刷新可以提升速度,操作完成后记得设置为True
app.display_alerts = False
app.screen_updating = False

#路径  前边加r表示绝对路径
path = r"自己的excel.xls"
# #打开已有excel

app = xw.App(visible=False, add_book=False)
wb = app.books.open(path)
sheet = wb.sheets[0]
# 通过last_cell方式获取最下边且最右边的一个单元格。
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()

# 写入 JSON 数据
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)
data = readjson(r"自己的.json")

# 写入txt
str=""
for i in range(len(data)):
    dict2 = {}
    dict2 = data
    for key in dict2:
        str = str + "\n" + dict2[key]

with open("test.txt","w") as f:
    f.write(str)  
# 写入word
doc = docx.Document()
doc.add_paragraph(str)

doc.save('test.docx')

免费评分

参与人数 2吾爱币 +2 热心值 +2 收起 理由
苏紫方璇 + 1 + 1 我很赞同!
sd0076 + 1 + 1 热心回复!

查看全部评分

sd0076 发表于 2021-9-22 22:38
luky77 发表于 2022-3-23 09:17
kiligleo9 发表于 2023-3-14 20:52
谢谢楼主分享! 刚好需要
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-14 01:46

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表