From d5f81a10e3b051fe2da3eba7ff168ca5ed56876b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=B7?= <10402852@qq.com> Date: Sat, 9 Nov 2024 07:46:33 +0800 Subject: [PATCH] 'commit' --- Py/.idea/.gitignore | 8 +++ Py/.idea/Py.iml | 8 +++ .../inspectionProfiles/Project_Default.xml | 14 +++++ .../inspectionProfiles/profiles_settings.xml | 6 ++ Py/.idea/misc.xml | 4 ++ Py/.idea/modules.xml | 8 +++ Py/.idea/vcs.xml | 6 ++ Py/DocxTuBiaoAdd.py | 61 +++++++++++++++++++ Py/DocxTuBiaoRead.py | 49 +++++++++++++++ pom.xml | 18 +++--- .../com/dsideal/base/Test/TestReadWord.java | 32 ++++++++++ 11 files changed, 207 insertions(+), 7 deletions(-) create mode 100644 Py/.idea/.gitignore create mode 100644 Py/.idea/Py.iml create mode 100644 Py/.idea/inspectionProfiles/Project_Default.xml create mode 100644 Py/.idea/inspectionProfiles/profiles_settings.xml create mode 100644 Py/.idea/misc.xml create mode 100644 Py/.idea/modules.xml create mode 100644 Py/.idea/vcs.xml create mode 100644 Py/DocxTuBiaoAdd.py create mode 100644 Py/DocxTuBiaoRead.py create mode 100644 src/main/java/com/dsideal/base/Test/TestReadWord.java diff --git a/Py/.idea/.gitignore b/Py/.idea/.gitignore new file mode 100644 index 00000000..38a1d4cd --- /dev/null +++ b/Py/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/Py/.idea/Py.iml b/Py/.idea/Py.iml new file mode 100644 index 00000000..d9e6024f --- /dev/null +++ b/Py/.idea/Py.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Py/.idea/inspectionProfiles/Project_Default.xml b/Py/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 00000000..2432abe9 --- /dev/null +++ b/Py/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,14 @@ + + + + \ No newline at end of file diff --git a/Py/.idea/inspectionProfiles/profiles_settings.xml b/Py/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 00000000..105ce2da --- /dev/null +++ b/Py/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/Py/.idea/misc.xml b/Py/.idea/misc.xml new file mode 100644 index 00000000..87ecb920 --- /dev/null +++ b/Py/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Py/.idea/modules.xml b/Py/.idea/modules.xml new file mode 100644 index 00000000..5d26daad --- /dev/null +++ b/Py/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Py/.idea/vcs.xml b/Py/.idea/vcs.xml new file mode 100644 index 00000000..2e3f6920 --- /dev/null +++ b/Py/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Py/DocxTuBiaoAdd.py b/Py/DocxTuBiaoAdd.py new file mode 100644 index 00000000..5990f959 --- /dev/null +++ b/Py/DocxTuBiaoAdd.py @@ -0,0 +1,61 @@ +# pip install pywin32 +# https://blog.csdn.net/weixin_42927998/article/details/115086797 +import win32com +from win32com.client import Dispatch + +docApp = win32com.client.Dispatch('Word.Application') +docApp.Visible = True +docApp.DisplayAlerts = 0 +doc = docApp.Documents.Open('c:/1.docx') + +# 创建图表,图表的插入位置为预先在word文档中插入的书签,书签名为“插入图表位置” +shape_chart = doc.Shapes.AddChart2(Style=201, Type=51, Top=doc.Bookmarks("插入图表位置").Select()) +shape_chart.WrapFormat.Type = 7 # 设置图表为嵌入型 + +# 设置Word中的图表 +chart = shape_chart.Chart +# 图表数据对应的工作表 +worksheet = chart.ChartData.Workbook.Worksheets(1) +chart.SetSourceData("Sheet1!$A$1:$C$4") # 设置数据源范围 + +# 簇状柱形图测试数据 +chart_data = [["", "系列A", "系列B", "系列C", "系列D"], + [2020, 2, 4, 2, 3], + [2019, 4, 5, 3, 2]] + +# 清空工作表默认数据 +worksheet.Range("A1:D5").value = None + +# 填入测试数据 +for row_index, row in enumerate(chart_data): + for column_index, value in enumerate(row): + worksheet.Cells(row_index + 1, column_index + 1).Value = value + +chart.SetSourceData("Sheet1!$A$1:$E$3") # 设置数据源范围 + +# 设置图表样式示例 +chart.ChartTitle.Text = '测试标题' # 设置标题 +chart.FullSeriesCollection(2).Format.Fill.ForeColor.ObjectThemeColor = 10 # 设置系列2的填充颜色 + +chart.ChartData.Workbook.Close() # 关闭workbook窗口 + +doc.Save() +doc.Close() +docApp.Quit() + + +''' +Type +1:柱形图(Column) +2:折线图(Line) +3:饼图(Pie) +51:堆叠柱形图(Stacked Column) +52:堆叠线图(Stacked Line) +53:堆叠区域图(Stacked Area) +55:雷达图(Radar) +65:树状图(Treemap) +73:旭日图(Sunburst) +77:水桶图(Funnel) +109:散点图(Scatter) +183:气泡图(Bubble) +''' \ No newline at end of file diff --git a/Py/DocxTuBiaoRead.py b/Py/DocxTuBiaoRead.py new file mode 100644 index 00000000..7c2f6db5 --- /dev/null +++ b/Py/DocxTuBiaoRead.py @@ -0,0 +1,49 @@ +# pip install pywin32 +# https://blog.csdn.net/weixin_42927998/article/details/115086797 +import win32com +from win32com.client import Dispatch + +docApp = win32com.client.Dispatch('Word.Application') +# 是不是打Word显示 +docApp.Visible = False +docApp.DisplayAlerts = 0 +# doc = docApp.Documents.Open('c:/1.docx') +# doc = docApp.Documents.Open('c:/昭通市人口变化及其对教育的影响20240416.docx') +# doc = docApp.Documents.Open('c:/昆明市人口变化及其对教育的影响20240419.docx') +doc = docApp.Documents.Open('c:/红河哈尼族彝族自治州人口变化及其对教育的影响20240419.docx') + + +# 遍历文档中所有的文字段落,判断是不是以 图+数字开头 +idx = 1 +for para in doc.Paragraphs: + x = para.Range.Text.strip().replace("图 ", "图").replace(" ", " ") + if x.startswith("图"): + print(x) + idx = idx + 1 + +# 遍历文档中的所有内嵌形状 +idx = 1 +for inline_shape in doc.InlineShapes: + if inline_shape.Type == win32com.client.constants.wdInlineShapeChart: # 检查是否为内嵌图表 + shape = doc.InlineShapes(idx) + # 获取图表的标题,此项目中图表没有标题 + # print(shape.Chart.ChartTitle.Text) + sheet = shape.Chart.ChartData.Workbook.Worksheets("Sheet1") + # 行数 + row_size = sheet.UsedRange.rows.Count + # 列数 + col_size = sheet.UsedRange.columns.Count + # 遍历获取表格中的数据 + for i in range(1, row_size + 1): + for j in range(1, col_size + 1): + print(sheet.Cells(i, j).Value, end=" ") + print("") + print("") + # 下一个图表的索引号 + idx = idx + 1 +print(idx-1) + + +# 关闭文档和Word应用 +doc.Close() +docApp.Quit() diff --git a/pom.xml b/pom.xml index 5120a35c..f836df88 100644 --- a/pom.xml +++ b/pom.xml @@ -117,12 +117,6 @@ slf4j-api 2.0.16 - - - org.apache.poi - poi-ooxml - 5.2.3 - net.sf.json-lib @@ -284,7 +278,17 @@ jjwt 0.7.0 - + + + org.apache.poi + poi + 5.2.3 + + + org.apache.poi + poi-ooxml + 5.2.3 + com.zendesk diff --git a/src/main/java/com/dsideal/base/Test/TestReadWord.java b/src/main/java/com/dsideal/base/Test/TestReadWord.java new file mode 100644 index 00000000..b4c85165 --- /dev/null +++ b/src/main/java/com/dsideal/base/Test/TestReadWord.java @@ -0,0 +1,32 @@ +package com.dsideal.base.Test; + +import org.apache.poi.xwpf.usermodel.*; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.List; + +public class TestReadWord { + public static void main(String[] args) throws IOException { + String path="c:/昭通市人口变化及其对教育的影响20240416.docx"; + FileInputStream fis = new FileInputStream(path); + XWPFDocument document = new XWPFDocument(fis); + + // 遍历文档中的段落 + for (XWPFParagraph paragraph : document.getParagraphs()) { + for (XWPFRun run : paragraph.getRuns()) { + List pictures = run.getEmbeddedPictures(); + if (pictures != null) { + for (XWPFPicture pictureData : pictures) { + System.out.println( pictureData.getPictureData().getPictureType()); + System.out.println( pictureData.getPictureData().getFileName()); + } + } + } + } + + document.close(); + fis.close(); + } +}