# link = "../path/to/file/../folder/../"
#
# count = link.count("../")
#
# print(count)  # 输出：4
#
# my_list = [1, 2, 3]
#
# result = ','.join(map(str, my_list))
#
# print(type(result))  # 输出：1,2,3
import io
import os

import requests
import win32com.client as win32
import win32com

import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
'Cookie': 'Secure HttpOnly; __jsluid_h=622c9569b7d6db50280612dfbe457ec4; __jsluid_s=b76555cb5e11504bde71924ab7ff780a; _va_ref=%5B%22%22%2C%22%22%2C1693534257%2C%22https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3Dm-wPghSYa5m0rKj3FjcXWqp8gfXcvYwljt9y1RdmIrHXd1lAl4QhbIbXDjZR9hZY%26wd%3D%26eqid%3Da0f738ed0005f760000000036459cd31%22%5D; _va_id=afdeea114ed19176.1683278397.11.1693534257.1693534257.; _va_ses=*',
'Host': 'gzw.beijing.gov.cn',
'If-Modified-Since': 'Mon, 31 Jul 2023 13:33:44 GMT',
'If-None-Match': 'W/"64c7b838-9676"',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'none',
'Sec-Fetch-User': '?1',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.62',
'sec-ch-ua': '"Chromium";v="116", "Not)A;Brand";v="24", "Microsoft Edge";v="116"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"'}

# def doc_page(doc_bytes):
#     doc = Document(doc_bytes)
#     num_pages = len(doc.sections)
#     print("文档页数：", num_pages)
#     return num_pages
#
# file_href = 'https://gzw.beijing.gov.cn/xxfb/zcfg/202204/P020230106624599701195.doc'
# resp_content = requests.get(file_href,headers=headers,verify=False, timeout=20).content
# doc_bytes = BytesIO(resp_content)
# category = '.doc'
# if category == '.doc' or category == '.docx':
#     page_size = doc_page(doc_bytes)
    # print(page_size)

# from docx import Document
# import requests
# from io import BytesIO
# import docx2txt
# import docx
# # 获取远程Word文档内容
# url = "https://gzw.beijing.gov.cn/xxfb/zcfg/202204/P020230106624599701195.doc"
# response = requests.get(url,headers=headers,verify=False, timeout=20)
# doc_bytes = BytesIO(response.content)
#
# # 解析文档页数
# # doc = Document(doc_bytes)
# # num_pages = len(doc.sections)
# # print("文档页数：", num_pages)
#
# doc_file =  docx.Document('E:\kkwork\P020230106624599701195.docx')
# # text = doc_file[0].getText()
# # print("文档内容：", text)
#
# # 解析文档页数
# num_pages = len(doc_file.sections)
# print("文档页数：", num_pages)

# from docx import Document
# import requests
#
# # # 下载远程Word文档
url = "https://gzw.beijing.gov.cn/xxfb/zcfg/202204/P020230106624599701195.doc"
# response = requests.get(url,headers=headers,verify=False, timeout=20)
# # with open("remote_document.doc", "wb") as file:
# #     file.write(response.content)
#
# # 将doc转为docx格式
# import os
# os.system("textutil -convert docx remote_document.doc")
#
# # 解析文档页数
# doc = Document("remote_document.docx")
# num_pages = len(doc.sections)
# print("文档页数：", num_pages)

# def get_file_stream(file_url):
#     # 发送GET请求获取文件内容
#     response = requests.get(url,headers=headers,verify=False,timeout=10)
#     # 将文件内容保存为文件流
#     file_stream = io.BytesIO(response.content)
#     return file_stream
# def read_file_stream(file_stream):
#     # 创建一个Word应用程序对象
#     word_app = win32com.client.Dispatch("Word.Application")
#     # 将文件流保存为临时文件
#     temp_file = "E:/temp.doc"
#     with open(temp_file, "wb") as f:
#         f.write(file_stream.read())
#     # 打开临时文件
#     doc = word_app.Documents.Open(temp_file)
#     # 读取文件内容
#     # content = doc.Content.Text
#     # print(content)
#     # 计算文档的统计信息
#     doc.ComputeStatistics(2)  # 参数2表示统计页数
#     # 获取文档的页数
#     page_count = doc.BuiltInDocumentProperties("Number of Pages").Value
#     # 输出页数
#     print(f"页数：{page_count}")
#     # 关闭文件和应用程序
#     doc.Close()
#     word_app.Quit()
#     # 删除临时文件
#     os.remove(temp_file)
#     return page_count
# file_stream = get_file_stream(url)
# page_count = read_file_stream(file_stream)
# print(f"页数22：{page_count}")

import io
import inspect
import requests
import os
import win32com.client as win32
import win32com
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
header={
    'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'Accept-Encoding':'gzip, deflate, br',
    'Accept-Language':'zh-CN,zh;q=0.9',
    'Cache-Control':'no-cache',
    'Connection':'keep-alive',
    'Cookie':'Secure HttpOnly; __jsluid_s=8e1b254e40166ac092b3e137cacf4f09; _va_ses=*; _va_id=0c66e4a68ee344ba.1693453315.1.1693453317.1693453315.',
    'Host':'gzw.beijing.gov.cn',
    'Pragma':'no-cache',
    'Sec-Fetch-Dest':'document',
    'Sec-Fetch-Mode':'navigate',
    'Sec-Fetch-Site':'none',
    'Sec-Fetch-User':'?1',
    'Upgrade-Insecure-Requests':'1',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36',
    'sec-ch-ua':'"Chromium";v="116", "Not)A;Brand";v="24", "Google Chrome";v="116"',
    'sec-ch-ua-mobile':'?0',
    'sec-ch-ua-platform':'"Windows"'
}
url='https://gzw.beijing.gov.cn/xxfb/zcfg/202204/P020230106624599701195.doc'
def get_file_stream(file_url):
    # 发送GET请求获取文件内容
    response = requests.get(url,headers=header,verify=False,timeout=10)
    # 将文件内容保存为文件流
    file_stream = io.BytesIO(response.content)
    return file_stream
def read_file_stream(file_stream):
    # 创建一个Word应用程序对象
    word_app = win32com.client.Dispatch("Word.Application")
    # 将文件流保存为临时文件
    temp_file = "D:/temp.doc"
    with open(temp_file, "wb") as f:
        f.write(file_stream.read())
    # 打开临时文件
    doc = word_app.Documents.Open(temp_file)
    # 读取文件内容
    # content = doc.Content.Text
    # print(content)
    # 计算文档的统计信息
    doc.ComputeStatistics(2)  # 参数2表示统计页数
    # 获取文档的页数
    page_count = doc.BuiltInDocumentProperties("Number of Pages").Value
    # 输出页数
    print(f"页数：{page_count}")
    # 关闭文件和应用程序
    doc.Close()
    word_app.Quit()
    # 删除临时文件
    os.remove(temp_file)
    return page_count
file_stream = get_file_stream(url)
page_count = read_file_stream(file_stream)
print(f"页数22：{page_count}")

print('程序结束！')


