file_manager.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import asyncio
  2. import json
  3. from pathlib import Path
  4. import shutil
  5. from typing import Dict, Type, Any
  6. from openpyxl import load_workbook,Workbook
  7. from utils.file import read_file,save_to_file
  8. from src.excel_tools.writers import (
  9. ExcelWriterBase,
  10. CompetitiveAnalysisWriter,
  11. ProductInfoWriter
  12. )
  13. from src.manager import DbManager,StorageManager
  14. from utils.logu import get_logger
  15. from config.settings import OUTPUT_DIR
  16. from src.models.asin_model import AsinExtraResultModel
  17. from src.models.product_model import Product,CompetitorAnalysis
  18. from src.manager.core.db_mongo import BaseMongoManager
  19. logger = get_logger('excel')
  20. class ExcelFileManager:
  21. TEMPLATE_PATH = Path(f"{OUTPUT_DIR}/resource/文案制作-template.xlsx")
  22. """Excel文件协调管理器"""
  23. def __init__(self, output_path: str=None, template_path: str = None):
  24. self.output_path = Path(output_path)
  25. self.template_path = template_path or self.TEMPLATE_PATH
  26. self.writers: Dict[str, ExcelWriterBase] = {}
  27. self.s3_storage_manager = StorageManager()
  28. self.wb:Workbook = self._prepare_workbook()
  29. logger.info(f"{self.wb.sheetnames}")
  30. logger.info(f"{self.wb.worksheets}")
  31. def _prepare_workbook(self):
  32. """准备工作簿"""
  33. if not self.output_path.exists():
  34. shutil.copy(self.template_path, self.output_path)
  35. return load_workbook(self.output_path)
  36. def save_all(self):
  37. self.wb.save(self.output_path)
  38. self.wb.close()
  39. def write_competitive_sheet(self, competitor_analysis:Dict[str, CompetitorAnalysis], sheet_name: str = "竞品关键词调研", sheet_index: int = 0, overwrite: bool = False):
  40. if overwrite and sheet_name in self.wb.sheetnames:
  41. self.wb.remove(self.wb[sheet_name])
  42. if sheet_name not in self.wb.sheetnames:
  43. competitive_sheet_writer = CompetitiveAnalysisWriter(self.wb, sheet_name=sheet_name, sheet_index=sheet_index)
  44. competitive_sheet_writer.add_data(competitor_analysis)
  45. def load_s3_extract_data(self) -> list[AsinExtraResultModel]:
  46. return self.s3_storage_manager.load_s3_complete_extract_data()
  47. async def main():
  48. self = ExcelFileManager(r"G:\code\amazone\copywriting_production\output\resource\multi_data.xlsx")
  49. db_mongo = BaseMongoManager()
  50. asyncio.run(db_mongo.initialize())
  51. product = await Product.find_one(Product.basic_info.name == "电线保护套")
  52. extract_data_lsit = product.competitor_analysis
  53. self.write_competitive_sheet(product.competitor_analysis)
  54. # self.save_all()
  55. return
  56. competi_sheet = CompetitiveAnalysisWriter(excel_file.output_path)
  57. list_model = excel_file.db.get_asin_completed()
  58. competi_sheet.add_data(list_dict)
  59. competi_sheet.save()
  60. return
  61. logger.info(f"{list_dict}")
  62. for model in list_dict:
  63. json_path = model.extra_result_path
  64. asin = model.asin
  65. data = json.loads(read_file(json_path))
  66. return
  67. output_path = r"G:\code\amazone\copywriting_production\output\multi_data.xlsx"
  68. generator = CompetitiveAnalysisWriter(output_path)
  69. data = json.loads(read_file(json_path))
  70. for json_path, asin in json_files:
  71. generator.add_product(json_path, asin)
  72. generator.apply_formatting()
  73. generator.save()
  74. if __name__ == "__main__":
  75. asyncio.run(main())