Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
+
def parse_text(txt_file):
|
| 6 |
+
# 将上传的文件名作为输入参数
|
| 7 |
+
file_name = txt_file.name
|
| 8 |
+
filename_with_dot = file_name.rsplit('/', 1)[1]
|
| 9 |
+
print(filename_with_dot)
|
| 10 |
+
filename = filename_with_dot.split('.', 1)[0]
|
| 11 |
+
print(filename)
|
| 12 |
+
with open(file_name, "r") as f:
|
| 13 |
+
# 读取上传文件中的内容
|
| 14 |
+
content = f.read()
|
| 15 |
+
# 在这里进行文本解析
|
| 16 |
+
lines = content.split("\n")
|
| 17 |
+
numbered_lines = [(i + 1, line) for i, line in enumerate(lines)]
|
| 18 |
+
|
| 19 |
+
# 将解析结果转换为字符串输出
|
| 20 |
+
output_str = ""
|
| 21 |
+
for num, line in numbered_lines:
|
| 22 |
+
output_str += f"{num}. {line}\n"
|
| 23 |
+
filename = filename.split('.', 0)[0]
|
| 24 |
+
return filename, output_str
|
| 25 |
+
|
| 26 |
+
def process_text(text):
|
| 27 |
+
try:
|
| 28 |
+
sentences = re.split(r'\d+\.', text)
|
| 29 |
+
except Exception:
|
| 30 |
+
try:
|
| 31 |
+
sentences = re.split(r'\d+', text)
|
| 32 |
+
except Exception:
|
| 33 |
+
pass
|
| 34 |
+
sentences = [s.strip() for s in sentences if s.strip()]
|
| 35 |
+
return sentences
|
| 36 |
+
|
| 37 |
+
def pd_form(sentences):
|
| 38 |
+
rows = []
|
| 39 |
+
for i in range(len(sentences)):
|
| 40 |
+
row_number = i + 1
|
| 41 |
+
description = sentences[i]
|
| 42 |
+
row = {
|
| 43 |
+
"镜号": row_number,
|
| 44 |
+
"画面范例": '',
|
| 45 |
+
"时长": '',
|
| 46 |
+
"景别": '',
|
| 47 |
+
"画面说明": description,
|
| 48 |
+
"歌词": '',
|
| 49 |
+
"备注": '',
|
| 50 |
+
}
|
| 51 |
+
rows.append(row)
|
| 52 |
+
return rows
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def to_excel(t_file):
|
| 56 |
+
text_file, name_file = parse_text(t_file)[1], parse_text(t_file)[0]
|
| 57 |
+
output_file_name = name_file + '.xlsx'
|
| 58 |
+
txt = process_text(text_file)
|
| 59 |
+
rows = pd_form(txt)
|
| 60 |
+
writer = pd.ExcelWriter(output_file_name, engine='xlsxwriter')
|
| 61 |
+
df = pd.DataFrame(rows)
|
| 62 |
+
df.to_excel(writer, index=False, sheet_name="Sheet1")
|
| 63 |
+
writer.close()
|
| 64 |
+
return output_file_name
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
iface = gr.Interface(
|
| 68 |
+
fn=to_excel,
|
| 69 |
+
inputs=gr.inputs.File(label="请上传 txt 格式文件"),
|
| 70 |
+
outputs=gr.File(label="下载 xlsx 格式文档"),
|
| 71 |
+
title="txt - xlsx 分镜格式转换器",
|
| 72 |
+
description="说明: 如果转换不成功,请重试几次")
|
| 73 |
+
|
| 74 |
+
iface.launch()
|