t_pandas_excel_reader.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import sys
  2. from utils.file import extract_excel_text_from_url, read_excel_from_url, get_all_cells_text, read_excel, extract_excel_text_from_file
  3. def test_pandas_excel_reader():
  4. """
  5. 测试函数:使用模块化函数读取 Excel 文件并提取文本
  6. """
  7. # 测试 URL
  8. test_url = "http://s3.vs1.lan/public/amazone/copywriting_production/product/202508/1P镊子压刀.xlsx"
  9. print(f"正在读取 URL: {test_url}")
  10. # 方法1:使用组合函数直接提取文本
  11. print("\n=== 使用 extract_excel_text_from_url 函数 ===")
  12. all_cells_text_dict = extract_excel_text_from_url(test_url)
  13. # 打印所有工作表的内容
  14. for sheet_name, sheet_content in all_cells_text_dict.items():
  15. print(f"\n=== 工作表: {sheet_name} ===")
  16. print(sheet_content)
  17. # 将每个工作表的内容保存到单独的文件中
  18. with open(f'test_{sheet_name}.txt', 'w', encoding='utf-8') as f:
  19. f.write(sheet_content)
  20. # 同时保存所有工作表到一个文件中
  21. with open('test_all_sheets.txt', 'w', encoding='utf-8') as f:
  22. for sheet_name, sheet_content in all_cells_text_dict.items():
  23. f.write(f"=== 工作表: {sheet_name} ===\n")
  24. f.write(sheet_content)
  25. f.write("\n\n")
  26. print(f"\n共读取了 {len(all_cells_text_dict)} 个工作表")
  27. return
  28. # 方法2:分别调用两个函数
  29. print("\n=== 分别调用 read_excel_from_url 和 get_all_cells_text ===")
  30. excel_data = read_excel_from_url(test_url)
  31. if excel_data:
  32. print("Excel 文件读取成功!")
  33. # 提取所有单元格内容(包括空值)
  34. all_cells_text = get_all_cells_text(excel_data)
  35. print(all_cells_text)
  36. else:
  37. print("Excel 文件读取失败")
  38. def test_local_excel_files():
  39. """
  40. 测试函数:读取本地 Excel 文件并提取文本
  41. """
  42. # 指定的Excel文件列表
  43. excel_files = [
  44. r"G:\code\amazone\copywriting_production\output\generated_excels\extra-data-大型犬牙刷手柄款.xlsx"
  45. ]
  46. for file_path in excel_files:
  47. print(f"\n=== 正在读取文件: {file_path} ===")
  48. # 检查文件是否存在
  49. import os
  50. if not os.path.exists(file_path):
  51. print(f"文件不存在: {file_path}")
  52. continue
  53. # 方法1:使用组合函数直接提取文本
  54. print("\n--- 使用 extract_excel_text_from_file 函数 ---")
  55. all_cells_text_dict = extract_excel_text_from_file(file_path)
  56. if all_cells_text_dict:
  57. # 将所有工作表内容合并为一个markdown文件
  58. combined_markdown = ""
  59. # 打印所有工作表的内容
  60. for sheet_name, sheet_content in all_cells_text_dict.items():
  61. print(f"\n--- 工作表: {sheet_name} ---")
  62. print(f"内容预览 (前200字符): {sheet_content[:200]}...")
  63. # 将Excel内容转换为Markdown格式,与extra_excel_product_flow.py保持一致
  64. combined_markdown += f"## 工作表: {sheet_name}\n\n```\n{sheet_content}\n```\n\n"
  65. # 将合并的markdown内容保存到与原始文件同路径的markdown文件
  66. file_dir = os.path.dirname(file_path)
  67. file_name = os.path.basename(file_path).replace('.xlsx', '.md')
  68. output_file = os.path.join(file_dir, file_name)
  69. with open(output_file, 'w', encoding='utf-8') as f:
  70. f.write(combined_markdown)
  71. print(f"\n已保存到: {output_file}")
  72. print(f"共读取了 {len(all_cells_text_dict)} 个工作表")
  73. else:
  74. print("Excel 文件读取失败")
  75. # 方法2:分别调用两个函数
  76. print("\n--- 分别调用 read_excel 和 get_all_cells_text ---")
  77. excel_data = read_excel(file_path)
  78. if excel_data:
  79. print("Excel 文件读取成功!")
  80. print("md 所在目录: G:\code\amazone\copywriting_production\output\generated_excels ")
  81. print("提示词所在目录: G:\code\amazone\copywriting_production\output\resource")
  82. # 提取所有单元格内容(包括空值)
  83. all_cells_text = get_all_cells_text(excel_data)
  84. print(f"提取到 {len(all_cells_text)} 个工作表的文本内容")
  85. else:
  86. print("Excel 文件读取失败")
  87. print("=" * 80)
  88. if __name__ == "__main__":
  89. # test_pandas_excel_reader()
  90. test_local_excel_files()